Need help with an 301 htaccess redirect rule doing the following:
www.name.com/wordA/wordB/* to www.name.com/WordNEW/wordA/wordB/*
we are basically adding "WordNew".
I have three wordA and five wordB for a total of 15 path variations.
Spin this on for size:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/WordNEW [NC]
RewriteRule ^(.*)$ /WordNEW/$1 [L,R=301]
Broken down
RewriteCond %{REQUEST_URI} !^/WordNEW [NC]
Check the requested URI does not start (!^) with the same folder path (/WordNEW) as the final. If it does, you've already redirected once or you are already there. If you don't check then you can end up on a loop rewriting the path over and over again.
RewriteRule ^(.*)$ /WordNEW/$1 [L,R=301]
If it passes, grab the whole requested URI (^(.*)$) and prepend with the new folder path (/WordNEW/$1). Then flag this as the last rule (L) in the RewriteRule while redirecting under 301 (R=301).
if WordNEW is in a constant position my quick and dirty solution would be to split the url string on '/'. at index 1 I would prepend the string WordNEW/ . For a more feasible solution I will need a few moments to think.
Here’s a simpler rule:
RewriteRule !^WordNEW/ /WordNEW%{REQUEST_URI} [L,R=301]
Related
I'm trying to remove one entire variable (but only if it's equal to 1). How can I do this? Basically I want to remove the string "&page=1". Here's what I mean:
If page=1:
301 redirect from BEFORE to AFTER
BEFORE: https://www.fubar.com/search.php?cs=tt&st=ss&page=1
AFTER: https://www.fubar.com/search.php?cs=tt&st=ss
If page = 2 or any other number EXCEPT 1 (nothing is changed or forwarded - no 301):
BEFORE: https://www.fubar.com/search.php?cs=tt&st=ss&page=2
AFTER: https://www.fubar.com/search.php?cs=tt&st=ss&page=2
I've tried some suggestions and searches, but nothing seems to be working...
Try with below rule,
RewriteEngine On
RewriteCond %{QUERY_STRING} !page=1$
RewriteRule ^ https://www.fubar.com/search.php?%{QUERY_STRING}&page=1 [R=301,L,END]
RewriteCond %{QUERY_STRING} !page=1$
RewriteRule ^ search.php?%{QUERY_STRING}&page=1 [L]
You can use the following :
RewriteEngine on
# if &page=1
#redirect to remove the &page perm
RewriteCond %{THE_REQUEST} /search\.php\?cs=tt&st=ss&page=1\s [NC]
RewriteRule ^.+$ https://www.fubar.com/search.php?cs=tt&st=ss [NE,L,R]
This will redirect your url from https://www.fubar.com/search.php?cs=tt&st=ss&page=1 to the same url but without the page perameter at the end (ie : https://www.fubar.com/search.php?cs=tt&st=ss ) .
Note that R is a 302 temporary redirect flag . I am using this just to test the rule and avoid browser caches. You can change the R to R=301 to make the redirection permanent .
I found a solution HERE: htaccess 301 redirect rule to remove part of a query string from URLs, but leave the rest)
Here's the code I ended up using:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)&?page=1(.*)$
RewriteRule ^/?search\.php$ /search.php?%1%2 [R=301,NE]
I have problem when I try to redirect and rewrite together.
I have site example.com/show_table.php?table=12 (max 99 tables). I wanted nice links, so I got this .htacces rw rule:
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
Now are links something like example.com/table/12 - it's definitely OK. But I want all old links redirect to new format. So I use Redirect 301, I added to .htaccess this code:
RewriteCond %{REQUEST_URI} show_table.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
But when I visit example.com/show_table.php?table=12, I receive just redir-loop. I don't understant - the first is rewrite, the second is redirection, there ain't no two redirections. Do You see any error?
Thanks!
Instead of checking REQUEST_URI in the condition, you need to be checking in THE_REQUEST (which contains the full original HTTP request, like GET /show_table.php HTTP/1.1). When Apache performs the rewrite, it changes REQUEST_URI, so to the rewritten value, and that sends you into a loop.
# Match show_table.php in the input request
RewriteCond %{THE_REQUEST} /show_table\.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
# Do a full redirection to the new URL
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
# Then apply the internal rewrite as you already have working
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
You could get more specific in the %{THE_REQUEST} condition, but it should be sufficient and not harmful to use show_table\.php as the expression.
You'll want to read over the notes on THE_REQUEST over at Apache's RewriteCond documentation.
Note: Technically, you can capture the query string in the same RewriteCond and reduce it to just one condition. This is a little shorter:
# THE_REQUEST will include the query string so you can get it here.
RewriteCond %{THE_REQUEST} /show_table\.php\?table=([0-9]{1,2})
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
I have a little problem with htaccess redirection, I have an issue which i can't figure out.
www.old.com/sub should direct to: www.new.com/page/
BUT
all the subpages of www.old.com/sub (www.old.com/sub/1, www.old.com/sub/2), should redirect to www.new.com/category/1, www.new.com/category/2 etc. (insted of page)
But how?
What I tried (i assumed this should work, but it didn't):
RewriteEngine on
RewriteRule ^sub/$ http://www.new.com/page/ [R=301,L]
RewriteRule ^sub(/.*)?$ http://www.new.com/category/$1 [R=301,L]
note; it is part of a large list of redirections, so it should not affect other redirections in this htaccess file.
That work with:
RewriteEngine on
RewriteRule ^sub/?$ http://www.new.com/page/ [R=301,L]
RewriteRule ^sub/(\d+)/?$ http://www.new.com/category/$1 [R=301,L]
You can change (\d+) to (.+) if you do not use numbers only after sub/
I've got:
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
Which points page/([^/\.]+)/? to index.php?page=$1.
When I go to page/([^/\.]+)/, I don't want to see index.php (which is achieved from the above)
For the reverse, when I go to index.php, I want to see a visible 301 redirection to page/([^/\.]+)/.
How do I do this without causing an infinite loop... or do I only rely on canonical tags?
Update:
I got it happening in one direction (new to old), but not the other (Old to new)
RewriteRule ^([^/\.]+)-yum/?$ yum/?x=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)-yum/?$ yum/?x=$2&y=$1 [L]
You need to do a check against the %{THE_REQUEST} variable so that you are rewriting only when the actual request is for the index.php file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^&]+)([^\ ]*)
RewriteRule ^/?index\.php$ /page/%1/?%2 [L,R=301]
So if a browser requests http://yourdomain.com/index.php?page=qwe&someother=var
It should get redirected to: http://yourdomain.com/page/qwe/?someother=var
Full solution here:
RewriteCond %{QUERY_STRING} Variable=([A-Za-z]+)$
RewriteRule OldURL/$ /NewURL/%1? [L,R=301]
RewriteRule ^NewUrl/([^/\.]+)$ MappedNewLocationFolder/?Variable=$1 [L]
1st Line: Check (the name and number of) parameters are as expected
2nd Line: Check base path of parameter(s), which is OldURL is correct and 301 to NewUrl
3rd Line: Map NewUrl to actual location where file sits (not shown in browser)
(The question mark right at the end of 2nd line strips out the variables from appending to the NewUrl)
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 ^).