Forum Moderators: phranque

Message Too Old, No Replies

Rewriting URLs with htaccess, multiple parameters

         

rigel

12:09 am on Jan 27, 2022 (gmt 0)




I'm trying to rewrite something like this:

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

into

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

Using this Rewrite Rule

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


However, when I try this code in [htaccess.madewithlove.com...] it gives me

This rule was not met.

Also tried it on my website htaccess file with no result. I don't know where is the problem.

[edited by: phranque at 2:23 am (utc) on Jan 27, 2022]
[edit reason] exemplified urls [/edit]

phranque

2:27 am on Jan 27, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], rigel!

where did you put the .htaccess file?
if it is in the document root directory, the Pattern supplied in your RewriteRule wouldn't match your intended path since it must necessarily start with ^pages/article/...

rigel

3:03 am on Jan 27, 2022 (gmt 0)



htaccess is in the main folder, with index.html. article.html is in pages forder
mywebsite.com/htaccess
mywebsite.com/pages/article.html

And also I don't get why it doesn't work on [htaccess.madewithlove.com...] event when I remove the pages directory.

Out of all the info out there, this seemed the most well explained, at least for me
[developphp.com...]

This problem is bugging me for 2 days. I thought that Google's Android camera2 API was a dumpster fire, this is close to that.

rigel

4:27 am on Jan 27, 2022 (gmt 0)



UPDATE: I've found a workaround to my problem.

I moved the article.html page to the root folder, where htaccess was. Also, put <base href="/" /> right after <head> element because none of the CSS or javascript were loading.

After a few changes in javascript to extract the ID from the new URL, all is working now.

phranque

4:33 am on Jan 27, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



IMPORTANT: Please Use example.com For Domain Names in Posts Apache Web Server forum at WebmasterWorld [webmasterworld.com]

Also tried it on my website htaccess file with no result.

i would first try a test to see if you are matching the requested url.

something like this:
RewriteRule ^pages/article/([0-9]+)/([0-9a-zA-Z_-]+)$ - [G]

should give you a 410 status code in the response, assuming you requested something like:
https://example.com/pages/article/1/Title-Goes-Here

phranque

4:35 am on Jan 27, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



mywebsite.com/htaccess

note that the .htaccess file should be located in:
/.htaccess 

note the leading dot in the filename - it is important

rigel

3:00 pm on Jan 27, 2022 (gmt 0)



Using this in htaccess:

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]

and this in article.html right after <head>
<base href="/" />

All is working as intended. Someone helped me with that code :)