mod_rewrite 301 redirect not working - .htaccess

I'm trying to set up a rewrite that will redirect this URL:
/video-2011.php?video=150
to this:
/video/150/freeform-title-text-here/
I have the rewrite working using this line in my HTACCESS:
RewriteRule ^video/([0-9]+)/(.*)$ video-2011.php?video=$1 [L]
But as soon I add R=301 into the mix, it breaks. Any ideas?
Here's the full HTACCESS when it breaks:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^video/([0-9]+)/(.*)$ video-2011.php?video=$1 [R=301,L]
Any help is greatly appreciated, thanks!

I think you might be missing a line from your .Htaccess.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^video/([0-9]+)/([a-z0-9-_]+)$ video-2011.php?video=$1 [L]
#New
RewriteCond %{REQUEST_URI} !^/video/([0-9]+)/([a-z0-9-_]+)/?$
RewriteCond %{QUERY_STRING} ^video=([0-9]+)&name=([a-z0-9-_]+)/?$
RewriteRule ^video-2011.php$ video/%1/%2/? [R=301,L]
I assuming you want to rewrite the url if it is:
/video/150/freeform-title-text-here/
And redirect the url if it is:
/video-2011.php?video=150
to:
/video/150/freeform-title-text-here/
So this way it keeps the urls looking pretty and tidy.
Please correct me if I am wrong.
Edit
I've added in a RewriteCond to stop the second rewrite happening.
As the first rule will obviously rewrite:
/video/150/freeform-title-text-here/
Which means the query string you don't see:
/video-2011.php?video=150
Would of made the second rule happen too.

Can you try:
RewriteRule ^video/([0-9]+)/ /video-2011.php?video=$1 [R=301,L,NC,QSA]
And yes it will redirect /video/150/foo to /video.php?video=150 not the other way around as you've stated in your question.

Related

.htaccess Redirect 301 with Rewriterule not working

When I try to redirect my page, nothing happens. If anyone tries to enter forum.example.com/forumdisplay.php?fid=1 I want them to be redirected to forum.example.com/forum-1.php. I am new to .htaccess and couldn't figure it out.
.htaccess file:
Options -MultiViews
RewriteEngine on
Redirect 301 /forumdisplay.php?fid=([0-9]+).php /forum-$1.php
RewriteRule ^forum-([0-9]+)\.php$ forumdisplay.php?fid=$1 [L,QSA]
Any help will be appreciated.
Redirect (mod_alias) and RewriteRule (mod_rewrite) belong to two different modules. You need to use mod_rewrite only for this. Try something like the following:
Options -MultiViews
RewriteEngine on
# Redirect direct requests for the "real" URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^fid=([0-9]+)
RewriteRule ^forumdisplay\.php$ /forum-%1.php? [R=301,L]
# Internally rewrite back to the "real" URL
RewriteRule ^forum-([0-9]+)\.php$ forumdisplay.php?fid=$1 [L,QSA]
(Redirect doesn't use regex either.)
UPDATE: I've appended a ? onto the end of the first RewriteRule substitution (ie. /forum-%1.php?). By specifying an empty query string, it removes the query string from the request. Alternatively, you can use the QSD flag on Apache 2.4+

.htaccess rewrite rule with get params

I need to be able to rewrite http://example.com/staging/details/?postid=23 so that it becomes http://example.com/staging/grandhotelterduin
I am not sure how the .htaccess rule should be?
I have came up with something like this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^grandhotelterduin/(.*)$ ./details/?postid=23 [L,NC]
I am not sure I am doing this correctly, can you plz help
I want it such that the http://example.com/staging/grandhotelterduin/ get redirected to http://example.com/staging/details/?postid=23 but the url that appears in the browser is `http://example.com/staging/grandhotelterduin
Use this:
RewriteEngine On
RewriteRule ^grandhotelterduin$ /staging/details/?postid=23 [L]
It will give you the following URL:
http://sheetz.nl/grandhotelterduin
EDIT:
RewriteCond %{THE_REQUEST} /staging/details/?postid=([0-9]+) [NC]
RewriteRule ^ /staging/grandhotelterduin [L,R]

mod_rewrite rule for exact expression not working

I don't work to much with mod_rewrite so I could easily be just missing something simple. I want to match and replace a specific URL. The URL I want to rewrite would look like this:
http://www.sample.com/hello to http://www.sample.com/index.php?page=hello
My htaccess currently has the following rule in place:
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
However, when I implement the rule and I test it by trying to go to http://www.sample.com/hello or http://www.sample.com/hello/ I always get my 404 error document. ugh I feel like it should be simple and I must be missing something right there. Any help would be fantastic and much appreciated!
Here is my whole file for reference:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?sample\.com/ [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png) [NC]
RewriteRule ^(.*)\.(gif|jpe?g|png)$ sample.com/hotlink.png[R,NC,L]
RewriteCond %{HTTP_HOST} ^www.sample\.com$ [NC]
RewriteRule ^(.*)$ http://sample.com/$1 [R=301,L]
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
Thanks
Your code works perfeclty fine for me. Maybe you're missing
RewriteEngine On
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
or maybe mod_rewrite is not installed or loaded at all.
Look for
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
in your main apache configuration file httpd.conf
This is one of those times...
I cleared the htaccess file and only used the following couple lines and tested it out:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
#
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
I realized looking at the non custom 404 page that it referenced page.php and not index.php!
Not Found
The requested URL /page.php was not found on this server.
So turns out with all my edits I mistakenly typed page.php in the rule instead of index.php. I corrected it and the rule works. One of those times :)
Thanks all!

How can I redirect old pages with question marks in the URL?

I have read about htaccess redirect and rewrite on stackoverflow and on other sites and learned how to redirect simple pages and directories, but there are about 30 links remaining that I haven't been able to redirect. The reason appears to be because they contain "?" in the link's URL. I've tried the solutions posted but I haven't been able to make enough sense of them to succeed.
These work:
Redirect /Corpfleet.php htp://www.marketyourcar.cm/wraps.php
Redirect /drivers.php htp://www.marketyourcar.cm/drivers.php
Redirect /galleries.php htp://www.marketyourcar.cm/galleries.php
These do NOT work:
Redirect /ad.php?View=FAQ htp://www.marketyourcar.cm/advertiser-faqs.php
Redirect /ad.php?View=gallery htp://www.marketyourcar.cm/galleries.php
Redirect /ad.php?View=Materials htp://www.marketyourcar.cm/products-services.php
Yes, I know that the URL above is htp and .cm - I had to break it in order to make this post with my low reputation level.
If anyone can help with this I'd appreciate it.
Thanks
If you want to redirect from like:
site.com/index.php?blabla=1&id=32
to
site.com/contact.html
then use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} blabla=1&id=32
RewriteRule ^(.*)$ http://www.site.com/contact.html? [R=301,L]
</IfModule>
Redirect can't handle that. RewriteRule can. This should work.
RewriteEngine on
RewriteRule ^/ad\.php\?View\=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php [R=301,L]
RewriteRule ^/ad\.php\?View\=gallery$ http://www.marketyourcar.cm/galleries.php [R=301,L]
RewriteRule ^/ad\.php\?View\=Materials$ http://www.marketyourcar.cm/products-services.php [R=301,L]
Or try this:
RewriteEngine on
RewriteRule ^/ad.php?View=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php [R=301,L]
RewriteRule ^/ad.php?View=gallery$ http://www.marketyourcar.cm/galleries.php [R=301,L]
RewriteRule ^/ad.php?View=Materials$ http://www.marketyourcar.cm/products-services.php [R=301,L]
This might work:
Example:
RewriteEngine On
RewriteRule ^/ad.php?View=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php? [R=301,L]
Add a trailing ? to the substitution URL to remove the incoming query.

htaccess rewrite if no rewrite was provided

First off I'm not an expert in htaccess stuff, but i try to accomplish the following for some SEO fixes.
When the url is loaded, without any params/rewrite it should get some data attached before it continues.
For example:
http://www.domain.com >>> http://www.domain.com/en/
I thought the rewrite was right like this, but didn't work (500)
RewriteCond %{REQUEST_URI} !^(en|nl|de|etc)$
RewriteRule ^(.*)$ /en/ [L,R=301]
added
RewriteRule ^(/?[^/]+) /index.php?rewrite=1 [L] # tell php we got a rewrite
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ $1/en/ [NC]

Resources