I have a rewrite rule where if you type:
www.whatever.com/profile-one.php
that is the url you see, but the actual template page is:
www.whatever.com/profile.php
Here are the lines in htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)([^/])$ http://www.whatever.com/$1$2/ [R=301,L]
RewriteRule profile-(.*)\.php$ profile.php?name=$1
My problem is, if you visit a url like:
www.whatever.com/second/profile-one.php
It still tries to bring back the profile page, even though the profile.php template does not exist in the "second" directory. What would be the best way to prevent this please?
This is happening due to wrong regex here. You need to use start anchor in your 2nd rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)([^/])$ http://www.whatever.com/$1$2/ [R=301,L]
RewriteRule ^profile-(.*)\.php$ profile.php?name=$1 [L,QSA]
Related
I have a htaccess file which redirects all files to the root index.php file unless certain conditions are met.
The following code works perfectly and is used to provide placeholder images for uploaded media assets if the website is run locally. This is to not need to download all files from the website so that the repository only consists of files.
The files that the following rules match look like:
[\d]{1,}.(thumbnail|medium|large).[\w]+
What I want to do is match the middle group and use it in the redirect rule but cannot seem to get it to work with:
RewriteCond %{REQUEST_URI} data/applications/.*/media/image/.*/[\d]+\.(.*)\.[\w]+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ data/applications/app/www/img/temp.$1.jpg [L]
The original code is here:
RewriteCond %{REQUEST_URI} data/applications/.*/media/image/.*/[\d]+\.thumbnail\.[\w]+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ data/applications/app/www/img/temp.thumbnail.jpg [L]
RewriteCond %{REQUEST_URI} data/applications/.*/media/image/.*/[\d]+\.medium\.[\w]+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ data/applications/app/www/img/temp.medium.jpg [L]
RewriteCond %{REQUEST_URI} data/applications/.*/media/image/.*/[\d]+\.large\.[\w]+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ data/applications/app/www/img/temp.large.jpg [L]
How can i combine the 3 separate matches into one rule?
You can make a single rules as this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(data/applications)/.*/media/image/.*/\d+\.([^.]+)\.\w+$ $1/app/www/img/temp.$2.jpg [L]
I have the following code to forward the root index page like the following:
www.domain.com/index.php?foo=value -> www.domain.com/value
Here is the code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?foo=$1 [L,QSA]
The problem is this forwards all my index pages to the main page and I really only want this code to effect the root.
I figured it would be as simple as adding the domain as part of the rule but it didn't work for me.
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /index.php?foo=$1 [L,QSA]
I have something like the following page:
www.domain.com/games/?puzzle=value
I'd like for it to show up as
www.domain.com/games/value
Here is the rewrite I wrote for forwarding to the original domain that causes a 500 Internal Server Error:
#forward games/value to games/?puzzle=value
RewriteRule ^games/(.+?)/?$ /games/index.php?puzzle=$1 [L,QSA]
Would someone help me solve my issue, I'm really bad at rewrites.
EDIT:
I'm currently using the following to allow parameters from foo on my main page.
# Internally forward /value to /index.php?foo=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?foo=$1 [L,QSA]
If I remove that then the following will work for what I want, but I need both instances to work.
#forward games/value to games/?puzzle=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^games/(.+?)/?$ /games/index.php?puzzle=$1 [L,QSA]
That is due to looping since URL after rewrite is also matching your pattern ^games/(.+?)/?$.
You need to exclude real files/directories from this rewrite rule:
#forward games/value to games/?puzzle=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^games/(.+?)/?$ /games/index.php?puzzle=$1 [L,QSA]
# Internally forward /value to /index.php?foo=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?foo=$1 [L,QSA]
I'm looking for series of .htaccess statements that will convert the following urls
http://mysite.com/product to http://mysite.com/product.php
http://mysite.com/product/55 to http://mysite.com/product.php?id=55
http://mysite.com/category/38 to http://mysite.com/category.php?id=38
http://mysite.com/resources/car/19 to http://mysite.com/resources/car.php?id=19
http://mysite.com/resources/car/19?color=red&year=2013 to http://mysite.com/resources/car.php?id=19&color=red&year=2013
In other words, when rendering php files in my website, i want to drop the .php extension. If a url ends with a number, then i want to pass that as the id query string parameter. I also want to pass all the conventional query string parameters to my php my file like color and year.
I'm not sure how to construct such a .htaccess file.
ADDITIONAL NOTES
I'm currently using hte following, but it fails to take into consideration urls that trail with a number, and passing that along as id
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{QUERY_STRING} (.*)
RewriteRule . %{REQUEST_FILENAME}.php?%1 [L]
If I can do something like replace the trailing number in REQUEST_FILENAME in line two, thatwould be great.
First you need to make sure Multiviews is turned off. Then you'll need 3 sets of rewrite rules:
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+)$ /$1.php?id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^resources/([^/]+)/([0-9]+)$ /resources/$1.php?id=$2 [L,QSA]
You can be a little more specific if the URLs are actually just "product", "category" and "car", then you can just have:
Options -Multiviews
RewriteEngine On
RewriteRule ^product$ /product.php [L]
RewriteRule ^(product|category)/([0-9]+)$ /$1.php?id=$2 [L,QSA]
RewriteRule ^resources/car/([0-9]+)$ /resources/car.php?id=$1 [L,QSA]
John (the op) says:
This was the final .htaccess file i ended up with
RewriteEngine On
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\/([0-9]+)$ $1.php?id=$2&%1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)$ $1.php?%1 [L]
I migrated my site to a new platform & I want to map old URLs like this:
anecdotage.com/index.php?aid=13337
to this:
anecdotage.com/articles/13336/
[Note the offset of one in the IDs]
Can I do this in apache's mod_rewrite. Any help appreciated!
You can't do any math with mod_rewrite by itself, meaning you can match against 13337 and do something to it to change it to 13336 without employing some kind of `RewriteMap to externally subtract a number by 1, but this requires access to either server or vhost config.
So in the vhost/server config you have to setup the map:
RewriteMap subtract prg:/path/to/script/that/subtracts-by-one.sh
This subtracts-by-one.sh script takes a number as an input, subtracts 1 from it, then prints out the result. Then to use it in a rewrite rule:
# to make sure we aren't clobbering legit requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=([0-9]+)
RewriteRule ^/?index.php$ /articles/${subtract:%1}/? [L]
If you don't have access to rewrite maps, then you'll need to do it the hard way and enumerate all of your rewrites:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=12345
RewriteRule ^/?index.php$ /articles/12344/? [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=13337
RewriteRule ^/?index.php$ /articles/13336/? [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=19911
RewriteRule ^/?index.php$ /articles/19910/? [L]
etc.