0

I want to rewrite this type of URL

https://mywebsite.com/content/page.html?id=12&title=my-page-title

to

https://mywebsite.com/12/my-page-title.html

Using generateit.net/mod-rewrite/ I got the following rule:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ /content/page.html?id=$1&title=$2 [L]

But when tested on htaccess.madewithlove.com/ I get this:

The new url is https://mywebsite.com/content/page.html?id=content&title=page
8
  • That was the rule given by a Rewrite rule generator. I've also tried different rules with no results. I've tried to understand how the rewrite rules work, but I didn't find any tutorial useful for me. They all seem to miss the key points in explaining how this works, or it's just a problem with my logic...
    – cat15ets
    Jan 26 at 19:14
  • 1
    (a) I looked at the generator site you named. You misinterpreted what it proposes to do. When it says "turn dynamic URLs into search engine friendly static URLs" it means that from the visitor's point of view. (b) The working of a rewrite rule is simply: try to match the incoming URL to the regular expression given as the first argument to the RewriteRulestatement, and if it does, replace it by the string given as the second argument. Jan 26 at 19:42
  • I don't understand what you say I did wrong. Go to: generateit.net/mod-rewrite , put mywebsite.com/content/page.html?id=12&title=my-page-title in the URL field, and click REWRITE URL
    – cat15ets
    Jan 26 at 19:47
  • The Mod Rewrite Generator can be used to turn dynamic URLs into search engine friendly static URLs. Static URLs typically rank better in search engines than long complex dynamic URLs with lots of parameters, they are easier to index in the first place and they're also friendlier looking to the end user. The apache module mod_rewrite converts urls in a certain format to another format, and can be very useful in helping a site with dynamic content to be indexed. Using this tool you can transform long dynamic URLs into short static URLs.
    – cat15ets
    Jan 26 at 19:56
  • Don't know what I didn't understand from that. I want a RULE that will turn long dynamic URLs into short static URLs
    – cat15ets
    Jan 26 at 19:59

2 Answers 2

1

The term "rewrite" refers to a process which has as its input the URL requested by your website visitor, and as its output the URL passed to your CMS to produce the requested page.

The service at https://www.generateit.net/mod-rewrite/ generates a rewrite rule allowing your pages to be reached through URLs of the form

https://mywebsite.com/12/my-page-title.html

even though the underlying system expects URLs of the form

https://mywebsite.com/content/page.html?id=12&title=my-page-title

It does that by rewriting the former to the latter, which is exactly what the rewrite rule quoted in your question does.

To test that with the service at https://htaccess.madewithlove.com/, you have to enter

https://mywebsite.com/12/my-page-title.html

in the field labeled "Fill in the url that you're applying the rules to". It would seem that you entered the rewrite result there instead.

2
  • And what rule I must use in my htaccess to get mywebsite.com/12/my-page-title.html from mywebsite.com/content/page.html?id=12&title=my-page-title
    – cat15ets
    Jan 26 at 21:11
  • @cat15ets You don't do that in .htaccess. You must manually (or in your CMS) change the URLs in your HTML source to be of the form example.com/12/my-page-title.html. However, having said that, you can (optionally) implement an external redirect to convert 3rd party requests from /content/page.html?id=12&title=my-page-title to the desired canonical URL (eg. /12/my-page-title.html) - but this is for SEO only, if you have changed an existing URL structure that has been indexed/linked to. This is not required for your website to function with the desired "pretty" canonical URLs.
    – MrWhite
    Jan 26 at 21:58
0

Thank you RavinderSingh13, from StackOverflow, for helping me with the code.

To turn

https://mywebsite.com/pages/article.html?id=1&title=Title-Goes-Here

into

https://mywebsite.com/pages/article/1/Title-Goes-Here

Use

RewriteEngine ON
##External redirect in browser rules here....
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/([^/]*)/([^.]*)\.html\?id=([^&]*)&title=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]

##Internal rewrite to html file rules here....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$  $1/$2.html?id=$3&title=$4 [QSA,NC,L]

Also, after head element, I had to put:

<base href="/" />

If you have atricle.html in the main folder, with htaccess you can also use:

RewriteEngine on
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$ article.html?id=$1&title=$2 [NC,L] 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.