1

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
cat15ets
  • 21
  • 1
  • 3
  • 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 '22 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 `RewriteRule`statement, and if it does, replace it by the string given as the second argument. – Tilman Schmidt Jan 26 '22 at 19:42
  • I don't understand what you say I did wrong. Go to: https://www.generateit.net/mod-rewrite/ , put https://mywebsite.com/content/page.html?id=12&title=my-page-title in the URL field, and click REWRITE URL – cat15ets Jan 26 '22 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 '22 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 '22 at 19:59
  • Define what you mean by 'turn into'. – Tilman Schmidt Jan 26 '22 at 20:00
  • From their website: **Using this tool you can transform long dynamic URLs into short static URLs. ** Transform, turn into, rewrite, take it how you want. I maybe too dumb for this or there are other things in play here ... – cat15ets Jan 26 '22 at 20:02
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/133536/discussion-between-tilman-schmidt-and-cat15ets). – Tilman Schmidt Jan 26 '22 at 20:05

2 Answers2

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.

Tilman Schmidt
  • 4,101
  • 12
  • 27
  • And what rule I must use in my htaccess to get **https://mywebsite.com/12/my-page-title.html** from **https://mywebsite.com/content/page.html?id=12&title=my-page-title** – cat15ets Jan 26 '22 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 '22 at 21:58
1

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] 
cat15ets
  • 21
  • 1
  • 3