Score:0

Htaccess to rewrite URL with two parameters

cn flag

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 avatar
cn flag
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...
Tilman Schmidt avatar
bd flag
(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.
cat15ets avatar
cn flag
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 avatar
cn flag
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 avatar
cn flag
Don't know what I didn't understand from that. I want a **RULE** that will turn **long dynamic URLs** into **short static URLs**
Tilman Schmidt avatar
bd flag
Define what you mean by 'turn into'.
cat15ets avatar
cn flag
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 ...
Tilman Schmidt avatar
bd flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/133536/discussion-between-tilman-schmidt-and-cat15ets).
Score:1
bd flag

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.

cat15ets avatar
cn flag
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**
kz flag
@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.
Score:0
cn flag

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] 
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.