link redirection using htaccess RewriteRule - .htaccess

I have these links in my website:
www.example.org/folder/files.php?file=folder/document.pdf
www.example.org/folder/files.php?force&file=2009.pdf
and I want redirect to :
www.example.org/files/folder/document.pdf
www.example.org/files/2009.pdf
I tried :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]
</IfModule>
but doesn't work!
any help?

RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]
There are two issues with this rule ... first, what you are matching needs to appear first in the rule, then what you are rewriting appears second - you have that backwards.
Once you reverse that, though, you run into the second issue - you can't match query strings in a RewriteRule, you need to match them in a RewriteCond:
To match www.example.org/folder/files.php?force&file=2009.pdf and redirect it to www.example.org/files/2009.pdf you would do:
RewriteCond %{QUERY_STRING} ^force&file=(.*)$ [NC]
RewriteRule ^folder/files.php$ /files/%1 [R=301, L]
The %1 matches what's in the parentheses in the RewriteCond.

Search on google first. The first thing displayed on google for htaccess is htaccess redirect. I think
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html (same line with a space) should work. Go to http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F . Php would also do the work. Just goolgle things before asking them.

Related

Troubleshooting mod_rewrite in a .htacces with a LightSpeed

I have a couple web pages located at these locations:
Home Page / Index : www.codeliger.com/index.php?page=home
Education : www.codeliger.com/index.php?page=home&filter=1
Skills: www.codeliger.com/index.php?page=home&filter=2
Projects: www.codeliger.com/index.php?page=home&filter=3
Work Experience: www.codeliger.com/index.php?page=home&filter=4
Contact : www.codeliger.com/index.php?page=contact
I am trying to rewrite them to prettier urls:
codeliger.com/home
codeliger.com/education
codeliger.com/skills
codeliger.com/projects
codeliger.com/experience
codeliger.com/contact
I have confirmed that my htaccess file works and mod-rewrite works to google, but I cannot get my syntax working that was specified in multiple tutorials online.
RewriteEngine on
RewriteRule /home /index.php?page=home
RewriteRule /([a-Z]+) /index.php?page=$1
RewriteRule /education /index.php?page=home&filter=1
RewriteRule /skills /index.php?page=home&filter=2
RewriteRule /projects /index.php?page=home&filter=3
RewriteRule /experience /index.php?page=home&filter=4
How can I fix my syntax to rewrite these pages to prettier urls?
The first thing you should probably do is fix your regex. You cannot have a range like [a-Z], you can just do [a-z] and use the [NC] (no case) flag. Also, you want this rule at the very end since it'll match requests for /projects which will make it so the rule further down will never get applied. Then, you want to get rid of all your leading slashes. Lastly, you want a boundary for your regex, otherwise it'll match index.php and cause another error.
So:
RewriteEngine on
RewriteRule ^home /index.php?page=home
RewriteRule ^education /index.php?page=home&filter=1
RewriteRule ^skills /index.php?page=home&filter=2
RewriteRule ^projects /index.php?page=home&filter=3
RewriteRule ^experience /index.php?page=home&filter=4
RewriteRule ^([a-z]+)$ /index.php?page=$1 [NC]

htaccess redirect with parameters not working

I'm struggling with an Apache rewriterule. I need to do the following:
Redirect permanently:
http://domain.com/folder/viewer/data/settings.xml?prevent_cache=4760
to
http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=4760
I've got the code below, it works without the url parameters but I can't seem to get it to work with parameters. Am i missing something?
RewriteCond %{QUERY_STRING} ^prevent_cache=([0-9]*)$
RewriteRule ^/folder/viewer/data/settings.xml$ http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R=301,L]
Cheers
Shaun
The only error I can see, is the leading slash / in the RewriteRule pattern. This should be
RewriteCond %{QUERY_STRING} ^prevent_cache=[0-9]*$
RewriteRule ^folder/viewer/data/settings.xml$ /siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R,L]
You don't need to append the query string to the substitution URL, because this is done autmoatically.
When everything works as you expect, you can change R to R=301. Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
I can. Here are the rewrite condition and rule that you're looking for:
# once per htaccess file
RewriteEngine on
RewriteCond %{QUERY_STRING} prevent_cache=([0-9]*)
RewriteRule ^folder/viewer/data/settings.xml http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=%1 [R=301,L]
But please considered this answer about the [R=301] flag: https://stackoverflow.com/a/15999177/2007055

.htaccess: How to permanently redirect a dynamic address, with no-follow, do-not-index directive using .htaccess

I have the following problem. The link format below no longer exist:
photos.domain.com/web/poppic.php?n=[any number from 0 to the roof]
Ej,: photos.domain.com/web/poppic.php?n=30
I replaced it, a year ago, to just: photos.domain.com/
Years after, I am still having tons of 404 errors.
I need a permanent redirect, and an htaccess difective for search engines to do not follow and to no longer index that old link.
I tried rewriterule ^web\/poppic\.php?n=30 "http\:\/\/photos\.domain\.com" [R=301,L] will work. HOWEVER, I requires to write each line, from 0 to 9999999999999999999999999:
rewriterule ^web\/poppic\.php?n=0 "http\:\/\/photos\.domain\.com" [R=301,L]
rewriterule ^web\/poppic\.php?n=1 "http\:\/\/photos\.domain\.com" [R=301,L]
rewriterule ^web\/poppic\.php?n=2 "http\:\/\/photos\.domain\.com" [R=301,L]
etc.
How can I do it with a variable, to replace php?n=[number] for php?n=$variable (Or something like that)?
Also, it is not telling crawlers to do not follow/index the page.
Could you please help?
You can't match against the query string inside of a rewrite rule using apache's mod_rewrite. You need to use a rewrite condition and match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^n=[0-9]+
RewriteRule ^web/poppic\.php$ /? [L,R=301]
Not sure how your original rule:
rewriterule ^web\/poppic\.php?n=30 "http\:\/\/photos\.domain\.com" [R=301,L]
ever worked. It doesn't work for me under apache 2.2 or 2.4.

.htaccess 301 redirect problem

i try to redirect url's like:
example.com/video/1640/video-name
to
example.com/video/1640/video-name/
i've tried with:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
but it is not working
my currently htaccess file has only the first line:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
and videos only loads at
example.com/video/1640/video-name/
url type
i want to redirect the non-backslash url type
example.com/video/1640/video-name
to the correct one (the one with the backslash)
How can i do this?
Your second rule should be RewriteRule ^video/([^/]*)/([^/]*)$ /video/$1/$2/ [R=301,L]
Or you could forgo the redirect totally, and just say RewriteRule ^video/([^/]*)/([^/]*)/?$ video.php?id=$1&title=$2 [L] which will allow both to view your video.
Update FallingBullets is right (see the comments on this answer), his answer better suites the OP's problem, so please ignore this answer (I am leaving it for reference, though).
Maybe you simply have to prefix your pattern with a /?? E. g.
RewriteRule ^/?video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^/?video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
# ^ these ones
instead of
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
since you are anchoring the pattern at the beginning of the path (using ^).

.htaccess redirect from GET variable to url string

I need to redirect
/search?keywords=somesearchterm
to
/search/somesearchterm
This seems incredibly basic but I've been pounding my head against it for an hour.
Thanks for taking the time to look at this.
You want to implement what is called a "301 Redirect" with mod_rewrite.
RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm
adding regular expressions:
RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]
R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.
If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:
RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]
You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :
The following rule works fine for this redirection
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
RewriteRule ^ /search/%1? [NE,NC,R,L]
RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]

Resources