Trying to make a redirection URL in htaccess.
I want to redirect URLs like
www.domain.com/pageANYTHING
to
www.domain.com
But I have an exception : when I got this
www.domain.com/page.phpANYTHING
do nothing.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^\.phpANYTHING [NC]
RewriteRule ANYTHING$ http://www.domain.com/? [L,R,NC]
Try this:
RewriteEngine On
RewriteRule ^page(?!\.php).*$ / [R=301,L]
In fact It works with a mix of your two propositions.
RewriteCond %{REQUEST_URI} !\.(?:php)$
RewriteRule ^page(?!\.php).*$ / [R=301,L]
Thank you so much guys.
Related
I'm trying to rewrite my website URLs. I have:
http:// website.com/v2/message.php?ID2=123
I want:
http:// website.com/v2/message-123.php
I tried something in my htaccess but I've a redirection loop :'(
Here is my .htaccess:
RewriteCond %{QUERY_STRING} ^ID2=([0-9]+)$
RewriteRule message.php? http://website.com/v2/message-%1.php? [L,R=301]
Can someone help me with this?
I suggest not using .php in pretty URL, make it extension-less. Try these rules in v2/.htaccess:
RewriteEngine On
RewriteBase /v2/
RewriteCond %{THE_REQUEST} /message\.php\?ID2=([^&\s]+) [NC]
RewriteRule ^ message-%1? [NE,R=302,L]
RewriteRule ^message-(.+)/?$ message.php?ID2=$1 [L,QSA,NC]
I have a website that uses the following format of links:
http://www.website.com/section1/index.php
http://www.website.com/section2/index.php
http://www.website.com/section3/index.php
http://www.website.com/section1/section4/index.php
What I was trying to do is to get rid of the last part "index.php" by using the following .htaccess directives:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteRule (.*) http://www.google.com [R=301,NC,L]
Of course, www.google.com is just for testing purposes, however the example below doesn't work. What is wrong with it? The second part of the question is what do I replace www.google.com with if I want to rewrite to http://www.website.com/section1 ?
Thank you!
It is not working because your regex is incorrect. RewriteCond %{REQUEST_URI} ^/index\.php$ is expecting %{REQUEST_URI} to be /index.php but you have /section1/index.php.
Correct version will be:
RewriteEngine on
RewriteCond %{REQUEST_URI} /index\.php$
RewriteRule (.*) http://www.google.com [R=301,NC,L]
Or even:
RewriteEngine on
RewriteRule /index\.php$ http://www.google.com [R=301,NC,L]
I have this line of code:
RewriteRule ^account/?edit=([A-Za-z]+)$ /?goTo=account&act=edit_$1 [L,NC]
When I go to mysite.com/account/?edit=username it is supposed to refer to mysite.com?goTo=account&act=edit_username but it gives me error 404
Any help?
Thanks!
You cannot match QUERY_STRING using RewriteRule. That requires a RewriteCond like this:
This should work:
RewriteCond %{QUERY_STRING} (?:^|&)edit=([^&]*) [NC]
RewriteRule ^account/?$ /?goTo=account&act=edit_%1#something [L,NC,NE,QSA]
Reference: Apache mod_rewrite Introduction
I would like to redirect all pages like:
www.mydomain.com/test
www.mydomain.com/test2/test3
and so on ...
to always base
www.mydomain.com
How can i do this?
RewriteCond %{HTTP_HOST} ^www.mydomain.com
RewriteCond %{THE_REQUEST} ^/(.*)$
RewriteRule (.*) http://%{HTTP_HOST} [L,QSA,R=301]
wont work
RedirectMatch 301 ^/ http://www.mydomain.com/
It will redirect everything to your new domain.
This will work if you have mod_alias
Please try this :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([a-z].*)
RewriteRule .* http://kap.com/ [R,L]
I find that solution OK
RewriteCond %{HTTP_HOST} ^www.mydomain.com
RewriteCond %{REQUEST_URI} ^/[a-zA-Z0-9\/]+$
RewriteRule ^ / [R,L]
Just leave out the RewriteConds and redirect everything to /
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ / [R,L]
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.
When I use this in my htaccess file:
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/ [R=301]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)$ $1.php [NC,L]
If you go to site.com without www it redirects to www.site.com/.php instead of www.site.com.
Any thoughts?
Thanks!
Maybe it's browser's cache from your old .htaccess? Try to empty the cache, or use other browser (or maybe Priavte Browsing).
I think at the root SCRIPT_FILENAME would be "/", which would not be considered a valid file and trip the !-f causing the rule to be applied.
Since the / is a character caught by the regex it will rewrite it to www.site.com/.php
Try to add "last" to rule by adding ",L":
RewriteRule ^(.*)$ http://www.site.com/ [R=301,L]
I ended up fixing it by adding $1 to RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
Thanks for all your help.