htaccess 301 redirect to rewriterule - php

I am changing my site URLs with .htaccess.
Until now, I was using
RewriteRule ^folder/([^/.]+)/p([0-9]+)/?$ folder/handler.php?name=$1&p=$2 [L]
Now, I want to change the "/p" for "-p".
I tried
RewriteRule ^folder/([^/.]+)/p([0-9]+)/?$ ^folder/([^/.]+)-p([0-9]+)/?$ [L, R=301]
But it gives me a 500 error when I try to access any of the two urls.
Is there any way to make a 301 from the first to the second one? Or any other choice?

Couple of issues:
You have space before R=301
On RHS (target side) of RewriteRule you have a regex
Keep your rules like this:
RewriteRule ^(folder/[^/.]+)/(p[0-9]+/?)$ /$1-$2 [L,NC,R=301]
RewriteRule ^folder/([^.-]+)-p([0-9]+)/?$ folder/handler.php?name=$1&p=$2 [L,NC,QSA]

This is how i do:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www*******com$ [NC]
RewriteRule ^(.*)$ www.******.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ www.*******.com/ [R=301,L]
Redirect /..../p /....-p

Related

I want to redirect specific url to non https using htaccess

my current htaccess is:-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
i want to add rule to htaccess for url like https://www.example.com/abc/arg1/arg2 should redirect to http://www.example.com/abc/arg1/arg2
for this https://www.example.com/abc/* it should redirect to non-https format with keeping all arguments.
Use below rule, check after clearing your cache.
RewriteCond %{REQUEST_URI} ^/abc/(.*) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Edit: Please try with below rule,
RewriteCond %{REQUEST_URI} !^/abc/(.*) [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In above rule we are appending https to all url which are not starting with abc/(.*) you have to use this rewritecond where your original https rule exists.
If you want to redirect all https requests starting with abc/ to http, you must check for both abc/ and HTTPS
RewriteCond %{HTTPS} on
RewriteRule ^abc/ http://www.example.com%{REQUEST_URI} [R,L]
When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.
I am not sure how you achieve the job, but the following should be working perfectly for you.
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/abc [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

.htaccess 301 redirect Apache

I am really having trouble with this Rewrite Rule
RewriteRule ^index.php?a=profile&u=(.*)$ /profile/$1 [R=301]
I want to make 301 redirect like
mysite.com/profile/USERNAME
Querystring is not part of match in RewriteRule, you need to match against %{THE_REQUEST} var in RewriteCond and then use %n to get that match :
This should be your complete htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} /index\.php\?a=profile&u=([^\s]+) [NC]
RewriteRule ^ /profile/%1? [L,R]
RewriteRule ^profile/([^/]+)/?$ /index.php?a=profile&u=$1 [NC,L]
Try
RewriteCond %{QUERY_STRING} ^a=profile&u=(.*)$
RewriteRule ^index.php /profile/%1 [L,R]

.htaccess redirecting to 404 error

Problem:
I want to redirect from www.domain.com/index.php?mod=daily to www.domain.com/daily_rent
While redirecting from www.domain.com/advert.php?id=11 to www.domain.com/advert/11 works fine like this:
RewriteCond %{THE_REQUEST} advert\.php\?id=([0-9]+)\ HTTP/
RewriteRule ^advert\.php$ http://www.domain.com/advert/%1? [R=301,L]
the same rule
RewriteCond %{THE_REQUEST} index\.php\?mod=daily\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/daily_rent? [R=301,L]
redirects to www.domain.com/daily_rent and throws 404 error. What could be the reason?
UPD:
Redirecting from www.domain.com/index.php?mod=daily to www.domain.com/daily_rent works fine, thanks anubhava
In that case another problem, on similar pages i have more than one query params, like this(example):
www.domain.com/?mod=daily&room=1&area=bstn
so, i try use that rule
RewriteCond %{THE_REQUEST} \?mod=daily&room=([0-9]+)&area=([A-Z]+)\ HTTP/
RewriteRule ^$ http://www.domain.com/daily_rent/room/%1/area/%2? [R=301,L]
RewriteRule ^daily_rent/room/([0-9]+)/area/([A-Z]+)/?$ /?mod=daily&room=$1&area=$2 [QSA,L]
what is the solution for that situation?
Thanks
Because you're missing internal rewrite rules. Try these rules:
RewriteCond %{THE_REQUEST} advert\.php\?id=([0-9]+)\s
RewriteRule ^advert\.php$ /advert/%1? [R=301,L]
RewriteCond %{THE_REQUEST} index\.php\?mod=(daily)\s
RewriteRule ^index\.php$ /%1_rent? [R=301,L]
RewriteRule ^(daily)_rent/?$ /index.php?mod=$1 [QSA,L]
RewriteRule ^advert/([^/]+)/?$ /advert.php?id=$1 [QSA,L]
Surely you want it the other way round - to redirect /daily_rent to /index.php?mod=daily? That would be a more common usage or mod_rewrite.
RewriteRule ^daily_rent$ /index.php?mod=daily [R=301,L]
You don't need the RewriteCond

Two redirect rules conflict with each other .htaccess

I had problems with a .htaccess file, that has been fixed, now i have another problem. I wish to clean the url using a .htaccess file. here is my current rule
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=page&b=([^&]+)[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=page&b=$1 [L,NC,QSA]
This will clean page links to point to a clean url that have a link containing a=page. It works perfectly. I tried to add another rule so that it applies to links containing a=rpofile but it did not work. However it does work if i remove the above code. It seems that i can not have both rules running at the same time. Below is the a=profile version of the above code.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=profile&u=([^&]+)[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=profile&u=$1 [L,NC,QSA]
If URL is /about then first rule will forward to /index.php?a=page&b=about now same /about cannot be forwarded to /index.php?a=profile&b=about.
But you can have /profile/something be treated as profile URL.
You can have 2 rules as:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=page&b=([^\s&]+)[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ /index.php?a=page&b=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=profile&u=([^\s&]+)[\s&] [NC]
RewriteRule ^ /profile/%1? [R=301,L]
RewriteRule ^profile/([^/.]+)/?$ /index.php?a=profile&u=$1 [L,NC,QSA]

Simple mod_rewrite rule not working [L]

So this is my first time doing mod_rewrite and I'm using tutorials and generators and it's been going okay so far except for the last line below which if I haven't made any mistake should redirect about.php to about/
Anybody know why it doesn't ?
All the other rules are working without issues (I did double check the page existed, I tried to add a slash before about.php as my website is on a server with multiple websites...)
Options +FollowSymLinks
#Sub-dir e.g: /cmsms
RewriteBase /
#redirect www.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.metareviewr.com
RewriteRule (.*) http://metareviewr.com/$1 [R=301,L]
#RewriteRule ^show/([^/]*)/$ /tvshow.php?id=$1 [L]
RewriteRule ^show/([^/]*)/([^/]*)/$ /tvshow.php?id=$1&?name=$2 [L]
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://metareviewr.com/$1 [R=301,L]
RewriteRule ^about/$ about.php [L]
This should work
RewriteRule ^about.php$ about/ [L,R=301]

Categories