I want to route all .php and .html files to a certain php file wether they have URL parameters or not.
Example:
www.mydomain.com/mysite.php?par1=test
www.mydomain.com/intro.html?nopar=1
Idea:
RewriteRule \.(?:php|html)$ work.php
You're pretty close, just add a condition to avoid rewriting if request is already for work.php:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/work\.php$ [NC]
RewriteRule \.(?:php|html)$ work.php [L,NC]
Note that query string is automatically carried over to target URI.
Related
I try to write a RewriteRule in the .htaccess but I have problems
I try to redirect from:
blog/entrada.php?id=2
To:
/blog/3D-touch
This is one of the multiple things I tried and does not work:
RewriteRule ^blog\/entrada\.php\?id=2$ /blog/3D-touch [L,R=301]
What is wrong with my Rule. How to redirect effectively?
Thanks
Querystring is not part of match in RewriteRule directive, to redirect query strings, you need to use RewriteCond one of the following options :
option 1
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/entrada\.php\?id=2 [NC]
RewriteRule ^ /blog/3D-touch? [NC,L,R]
option 2
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=2$ [NC]
RewriteRule ^blog/entrada\.php$ /blog/3D-touch? [NC,L,R]
We use an empty question mark ? at the end of the target url to discard the old query strings, otherwise these query strings get appened to the target url by default.
Change the R to R=301 if you want to make the redirection permanent.
use from this code
RewriteRule blog/(.*) blog/entrada.php?id=$1
this code will redirect all urls which have blog/ to blog/entrada.php and put after value of blog/ to $_GET['id']
you should have following code n the top location of your htaccess file
Options +FollowSymLinks
RewriteEngine on
I need to write optimized .htaccess rules. I had written like that and they are working
RewriteEngine on
RewriteRule ^city1-name-in-url/$ products.php?city=city1-name-in-url
RewriteRule ^city2-name-in-url/$ products.php?city=city2-name-in-url
RewriteRule ^city3-name-in-url/$ products.php?city=city3-name-in-url
RewriteRule ^city4-name-in-url/$ products.php?city=city4-name-in-url
And url for these rules are http://www.example.com/global/city1-name-in-url/
I have to write these rules for 800 cities which is making website slow. I want to know if there is anyway to get the part url and use it in RewriteRule
Example like
If url : http://www.example.com/global/any-city-name/
RewriteRule ^any-city-name/$ products.php?city=any-city-name
This is possible? To get the part of url after global and then use it in rewrite rule.
You can use this rule in /global/.htaccess:
RewriteEngine On
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ products.php?city=$1 [L,QSA]
[\w-]+ matches 1 or more of [a-zA-Z0-9_-] characters.
So I have a legacy site archived with 1000s of image URLs like
img/2265/thumb/Incubator.jpg?1344891444
and the file it points at is sitting at:
img/2265/thumb/Incubator-1344891444.jpg
some of the files are jpegs, pngs etc, but they all follow this pattern of a hyphen then querystring value then extension. (keeping rest of path the same)
So far I have got the Querystring part but cannot figure out how to combine this with a RewriteRule (?) to keep the filename but add in the matched value.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^([0-9]*)$
RewriteRule PATTERN_TO_MATCHJPG|PNG|GIF$ SOMETHING-$1.jpeg [L]
Is it that the RewriteRule first part should be a rewritecond? :-/
thanks for any tips.
UPDATED to show answer in nice format!**
RewriteCond %{QUERY_STRING} ^([0-9]+)$
RewriteRule (.+?)\.(png|gif|jpg|jpeg|JPG|JPEG|PNG|GIF)$ $1-%1.$2? [L]
You can use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^([0-9]+)$
RewriteRule ^(.+?)\.(png|gif|jpe?g)$ /$1-%1.$2? [L,NC]
I've tried dozens of different ways of doing this but can't get any of them to work. My .htaccess does a few things, like setting a custom 404 and blocking image hotlinking. I want to do two things on the URL: add www. if it isn't there (rather annoying Facebook login can't cope with two different sources!), and replacing // with / except after http:.
I've tried this:
# Replace // with /
RewriteCond %{REQUEST_URI} (.*)(?<!http:)\/{2,5}(.*)
RewriteRule .* %1/%2 [R=301,L]
And this:
# Replace // with /
RewriteCond %{REQUEST_URI} (.*).com\/\/(.*)
RewriteRule .* %1.com/%2 [R=301,L]
And all sorts of permutations. Can anybody tell me what I'm doing wrong?
I need to do this because sometimes multiple /s are being inserted between the .com and the rest of the URL.
Thanks
I don't think http:// is part of REQUEST_URI at all (or of any other environment variable for that matter). It will get parsed out by the browser, and used to determine the nature of the request, long before the actual request is made.
I can be wrong, but I think this is not fixable on htaccess level. The link would have to be properly formatted in the first place.
Update: Looking at the information Apache passes on to PHP, I think I'm right. The protocol used to make the request is not part of the URI components we get to play with.
Here's how to force www.:
<IfModule mod_rewrite.c>
#Add WWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#End Add WWW
</IfModule>
Considering what #Tim mentioned below, I would check %{REQUEST_URI} if it contains //, and that would be my RewriteCond:
<IfModule mod_rewrite.c>
#Replace // with /
RewriteCond %{REQUEST_URI} // [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#End Replace // with /
</IfModule>
I'm not sure why you're experiencing trouble with the multiple slashes, since it should be able to resolve the file either way. However, it is possible to check for and remove them with a redirect (I've combined this with your force-www so there's at most one external redirection):
RewriteCond %{THE_REQUEST} ^[A-Z]+\s[^\s]*/{2,} [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteRule ^ http://www.%2%{REQUEST_URI} [R=301,L]
Note that %{REQUEST_URI} has the duplicate slashes removed (only in mod_rewrite, this isn't true for scripts later on), so we can use it in the redirect to automatically take care of that issue for us. The original request will still have the multiple slashes though, so we check for them by examining %{THE_REQUEST}.
I have a tricky issue redirecting some URLs internally for my site.
The situation is this, I currently have a URL like example.com/check/youtube.com which internally redirects to check.php?domain=youtube.com using the following mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteRule ^offline offline.php [NC,L]
RewriteRule ^error error.php [NC,L]
RewriteRule ^check/(.*)$ check.php?domain=$1 [NC,L]
However I would also like to be able to redirect to check.php using a URL like example.com/youtube.com. Unfortunately it is just beyond me to figure it out.
I have a directory /assets/ with all the CSS, JS, etc. which shouldn't be affected.
Thanks
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.]+\.[^/]+$ check.php?domain=$0 [L]
This rule rewrites any request with a URL path of the form [^/.]+\.[^/]+ (a string that contains at least one dot but no slashes at all) that cannot be mapped to an existing file to your check.php.
As you want to redirect "example.com/youtube.com" does that mean you wish to redirect pretty much anything? What is specifically allowed to be passed, e.g. would I be allowed to pass "example.com/youtube.com/foobar.php" for a redirect to check.php?