Want to forward the following:
localhost/bla/index.html -> localhost/index.php?s=bla/index.html
localhost/blub.html -> localhost/index.php?s=blub.html
localhost/blog.html?site=10 -> localhost/index.php?s=blog.html&site=10
localhost/folder -> localhost/index.php?s=folder
but I only want do redirect .html and folders.
RewriteCond %{REQUEST_URI} .html$ [OR]
RewriteCond -d
RewriteRule (.*) /index.php?s=$1 [L,QSA]
Only if the uri ends with ".html" or if it's a folder, it rewrites the entire uri into the ?s= query string. The QSA flag should make sure that query strings on .html files (?site=10) are added to the new url as well.
Related
I want to change
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
That is if the URL contains index.php?members, then I add /#div at the end of url. I tried this
RewriteEngine On
RewriteCond %{REQUEST_URI} index.php?
RewriteRule (.*) /%1/%{QUERY_STRING}&123 [L,QSA,R=301]
but it returns
domain.com/members/maxmusterman.5&123?members/maxmusterman.5
Note here that &123 is attached after URI before starting parameters. I researched htaccess QSA flag but I could not find a way to add a custom string at the end of the query string. How can I do that. Here I have used &123 for test purpose, actual requirement is adding /#div
To redirect
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
.
You can use something like the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} !loop=no
RewriteRule ^division1/index\.php$ %{REQUEST_URI}?%{QUERY_STRING}&loop=no#div [L,R,NE]
I added an additional perameter loop=no to the destination url to prevent infinite loop error .You can't avoid this as both your old url and the new url are identical and can cause redirect loop if you remove the RewriteCond and Query perameter.
NE (no escape ) flag is important whenever you are redirecting to a fragment otherwise mod-rewrite converts the # to its hex %23 .
solution #2
RewriteEngine on
RewriteCond %{THE_REQUEST} !.*loop=no [NC]
RewriteCond %{THE_REQUEST} /division1/index\.php\?(.+)\s [NC]
RewriteRule ^ /division1/index.php?%1&loop=no#div [NE,L,R]
Clear your browser cache before testing these redirects.
How to redirect the path using string compare.
For example: We have path like (1000+ redirections)
www.example.com/kb-123
www.example.com/kb-555
www.example.com/kb-666
Now using single rule how to redirect all above path's into below path's?
www.example.com/article/kb-123
www.example.com/article/kb-555
www.example.com/article/kb-666
Try with below rule,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)
RewriteRule ^ /article/%1 [R=301,L]
I want a redirect to happen if subfolder prefix is 'blog' and 'X' is not equal to 'party'.
This should not redirect
http://www.hostname.co.uk/blogs/blogparty
This should redirect
http://www.hostname.co.uk/blogs/blogX
to
http://www.hostname.co.uk/blogs/X
where 'X' is any string which does not equal to 'party'.
This should work on sub directories of this directory as well meaning this should be redirected too.
http://www.hostname.co.uk/blogs/blogX/blah/index.php
to
http://www.hostname.co.uk/blogs/X/blah/index.php
This is what i tried which is redirecting everything including 'blogparty'
# Redirect blogBLOGNAME to BLOGNAME excluding blogparty
RewriteCond %{REQUEST_URI} !^/blogs/blogparty/.*
RewriteCond %{REQUEST_URI} !^/blogs/blogparty$
RewriteRule ^/blogs/blog(.*) /blogs/$1 [R=301,L]
Your rules should have worked. I believe there are other routing rules below your shown rule. To handle this you can have your rule based on THE_REQUEST instead (which doesn't change after other rules):
RewriteCond %{THE_REQUEST} !\s/+blogs/blogparty(/\S*)?\s [NC]
RewriteRule ^(blogs)/blog(.+)$ /$1/$2 [R=302,L,NC,NE]
my goal is to write .htaccess rule "mvc like"
/param1/param2/param3/../paramN -> index.php?param1¶m2¶m3&...paramN
Excluding for
/css /img /js
ex:
mysite.com/login/john/white->mysite.com/index.php?login&john&white
mysite.com/css/style.css -> mysite.com/css/style.css (NO RULE APPLIED)
mysite.com/img/mypic.jpg -> mysite.com/img/mypic.jpg (NO RULE APPLIED)
If you need to rewrite the URL and not css, js, img files,
Then you can use this htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILNAME} !-f
RewriteCond %{REQUEST_FILNAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ http://example.com/index.php?$1&$2&$3 [QSA,L]
Which will rewrite this example URL.
example.com/index.php?login&john&white
Note that there are no key/value pairs in your query string so that might not come out as you might expect.
You can add aditional rules for longer or shorter URL's.
I'm new with 301 redirects through .htacces.
I can get simple redirects as
redirect 301 /test.html http://www.domain.com/test2.html
to work but I have some urls like this
redirect 301 /test.asp?Group=100 http://www.domain.com/test3.html
and for some reason these don't work.
Thanks.
Here is set of rules for URLs you have provided:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =group=113 [NC]
RewriteRule ^group\.asp$ http://domain.dk/til-born.htm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} =product=1136 [NC]
RewriteRule ^product\.asp$ http://www.domain.dk/til-born/bukser.html? [NC,R=301,L]
As you can see query string is matched separately to the page name. So .. for each of such redirects you need 2 lines: RewriteCond & RewriteRule.
The rule above will do EXACT match, which means /group.asp?group=113¶m=value will not be redirected because query string is group=113¶m=value which is more than just group=113.
To have such redirect working (when there are some optional parameters in query string) you have to modify it: RewriteCond %{QUERY_STRING} (^|&)group=113(&|$) [NC] -- this will match group=133 anywhere in query string (group=113 and group=11366 are still different, so no problems here).
This needs to be placed in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
The Redirect directive (as far as I know) matches only on the path, not querystring. Instead, use RewriteRule. The QSA instructs the rewrite engine to append the querystring onto the new redirected URL.
RewriteEngine On
RewriteRule ^test\.asp http://www.domain.com/test3.html [L,R=301,QSA]