.htaccess - mod-rewrite and redirecting profile URLs with parameters to clean ones - apache

I have links to user profiles like:
https://example.com/chat/index.php?action=user&member=547
https://example.com/chat/index.php?action=user&member=11540
etc.
My goal is to mod-rewrite such URLs so that they look nicer, ie. they should look like:
https://example.com/chat/member-547/
https://example.com/chat/member-11540/
etc.
And URLs without a trailing slash, ie.
https://example.com/chat/member-547
should be 301-forwarded to one with slash, ie.
https://example.com/chat/member-547/
So in .htaccess I tried this, but only the first line seems to work and it's not complete:
RewriteRule ^chat/member-([0-9]+)/$ ./index.php?action=user&member=$1
RewriteRule ^chat/member-([0-9]+)$ /member-$1/ [R=301,L]
TO SUMMARIZE:
When someone enters URL like:
https://example.com/chat/index.php?action=user&member=547
it should be 301-redirected to:
https://example.com/chat/member-547/
When someone enters:
https://example.com/chat/member-547
it should also be 301-redirected to:
https://example.com/chat/member-547/
I hope there's an efficient way to do it right.

Starkeen's answer is correct, and will work. I'll just suggest an alternative, which acts on the raw request variable itself:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /chat/index\.php\?action=user&(member)=(\d+) [NC]
RewriteRule ^chat/index\.php$ /chat/%1-%2/? [R=301,L]
RewriteRule ^chat/member-([0-9]+)$ /member-$1/ [R=301,L]
RewriteRule ^chat/(member)-(\d+)/$ /chat/index.php?action=user&$1=$2 [L]

You can use the following rule :
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^action=([^&]+)&member=(.+)$
RewriteRule /chat/index\.php$ /chat/member-%2/? [L,R=301]
RewriteRule ^chat/member-(.+)/?$ /chat/index.php?action=user&member=$1 [L]

Related

htaccess, rewrite for specific subdirectory but not others below it

I have a subdirectory /categories/stories/ that I want to rewrite to /books/ but I don't want to rewrite something like /categories/stories/horror. I'm doing the following but it has no effect:
RewriteCond ${REQUEST_URI} ^/categories/stories[/]?
RewriteCond ${REQUEST_URI} !^/categories/stories/+[/]?
RewriteRule ^(.*) /books/ [R=301,L]
The result being if a person goes to, for example, http://bookstore.com/categories/stories they go to http://bookstore.com/books but http://bookstore.com/categories/stories/horror there's not rewrite.
You can just use anchors in your regex pattern:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/categories/stories/?$ [NC]
RewriteRule ^ /books/ [R=301,L]
Pattern ^/categories/stories/?$ will match /categories/stories/ or /categories/stories but not /categories/stories/anything.
Also make sure to clear your browser cache or use a different browser for testing this change.

301 working on some URLs and not on others in htaccess

Hi I am having an issue with .htaccess on 301 redirects.
Everything looks fine, on 90% of the urls but not for any of the individual blog post redirects.
here is the code where they do not redirect:
RewriteRule ^blog-article.php?id=38 blog-post/xxxx-xxxxxx-xx-x-xxxxxxx/9/ [L,NC,R=301]
RewriteRule ^blog-article.php?id=34 blog-post/xxxxxxx/4/ [L,NC,R=301]
But the all of the other 301 rewrites work, and they look like this:
RewriteRule ^old-url-portfolio/(.*)$ page/new-url-portfolio/$1 [L,NC,R=301]
RewriteRule ^Portfolio/Detail/(.*)$ portfolio/$1 [L,NC,R=301]
RewriteRule ^old-url/7/ page/new-url/ [L,NC,R=301]
RewriteRule ^contact-us-old/ page/contact-us-new/ [L,NC,R=301]
RewriteRule ^blog.php page/blog/ [L,NC,R=301]
I can not find why they are not working, its not because the ones that are not working are individual files because the blog.php file is being sent to the new page.
Any ideas would be appreciated :)
You can not match against querystring in RewriteRule's pattern. To redirect blog-article.php?id=123 to diffrent location , you need to match against %{QUERY_STRING} using RewriteCond directive.
RewriteCond %{QUERY_STRING} ^id=123$
RewriteRule ^blog-article.php$ /location/? [L,R]
Note :The traling ? at the end of the destination path is important as it discards the old querystring.
Found a work around, it seams the issue is the ?id=number so I simply did this:
RewriteCond %{QUERY_STRING} ^id=38$
RewriteRule ^blog-article\.php$ http://www.xxxxxxx.co.uk/blog-post/xxxx-xxxxxx-xxxx/9/ [R=301,L]

Remove part of the query string with mod_rewrite

I am not very good with .htaccess at all, so I want to achieve something very simple, but I can't. What I want to do is to redirect certain files to test.php, and if test is ok, PHP redirects back to original page. It works fine, I add the "test=ok" part to the original URL, that way I don't get a redirect loop. However, I want to remove the test=ok query part from the original URL on redirection. How can I achieve that???
TL/DR
I have several URLs I want rewritten through mod_rewrite.
examples:
http://example.com/?time=1&test=ok
http://example.com/?test=ok
How can I remove the &test=ok and the ?test=ok parts using .htaccess?
Right now I have:
RewriteCond %{QUERY_STRING} ^test=ok$ [NC]
RewriteRule (.*) /$1? [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?|js))$ [NC]
RewriteCond %{QUERY_STRING} !test=ok [NC]
RewriteRule .* test.php [L]
But that doesn't remove the test=ok part... :(

Rewrite urls friendly with htaccess. how I can use it on my site?

I'm rewriting urls on htaccess using rewrite rules, but when I want that my site uses it I get stuck.
I want my links looks like:
mysite.com/section/this-is-the-title-of-this-section
I used rewrite rules to get this:
mysite.com/section/?id=longalfanum to mysite.com/section/longalfanum
that was cool for a moment. but I checked some few websites for the URLs I realize that they have friendly URL from the beginning
so I changed the url on the to looks like:
mysite.com/section/longalfanum-the-title-of-the-section
now my links doesn't work like before.
my htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#this is for avoid extra /
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
RewriteCond %{QUERY_STRING} !^$
RewriteRule .*/mysite.com/%{REQUEST_URI}? [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#this is my rule
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule /mysite.com/section/$1? [L]
RewriteRule ^section/(\d+)$ mysite.com/section/?id=$1
obviosly this doesn't work. am I missing a step?
Change
RewriteRule ^section/(\d+)$ mysite.com/section/?id=$1
to
RewriteRule ^section/(.*)$ mysite.com/section/?id=$1
so it matches anything (.*) after 'section/', not just digits (\d+)
Also, you should add an [L] flag to all RewriteRules that don't currently have it to make things a little bit more efficient.
my html with php looks like this:
in the web browser looks like:
mysite.com/section/?idN=alfanum
my rule looks like:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule /mysite.com/section/$1? [L]
RewriteRule ^section/(.*)$ mysite.com/section/?id=$1
with this I only get URL like mysite/section/nosensealfanum
it is possible to get the title of new by the htaccess? or I its need to make changes in the way I get the new from the db?
thanks

.htaccess url rewrite - remove .php remove www. convert ?id=10 to /10

As the title says, I need a quite complex url rewrite mechanism for a web-app as .htaccess rule.
I've searched quite a lot now and tried hundred of different rewrite rules.
So, basically this is what I need:
User goes to: http://www.site.com/product.php?id=12
Server should redirect to: http://site.com/product/12
Once thing to mention:
not all pages do append id's.
So I also have: http://www.site.com/some/page.php
which then should redirect to: http://site.com/some/page
or from http://site.com/anotherone.php to http://site.com/anotherone
You help is much appreciated and thank you a lot in advance for you help!
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)\.php$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)\.php\?id=([0-9]+)$ http://%1/$1/$2 [R=301,L]
I supose that you, previously have this mod_rewrite rule active:
Users goes to http://site.com/product/12 and in the browser is showed this URL, and internaly, and only internaly, server serve http://www.site.com/product.php?id=12
Put the first RewriteCond and Rule this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(\w+)\.php\?id=(\d*)$ /$1/$2 [R=301]
And add another to remove the .php when ends with .php
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(\w+)\.php$ /$1 [R=301]