htaccess 301 Redirection for Multiple Files - php

I'm wondering if multiple entries on htaccess will work for 301 redirects.
The problem I see is that old site files have 'html' extension and are named named differently so a simple global redirect won't work, it has to be one rule per file name.
This sites receives 1000 visits daily so need to be careful not to penalize search engine.
RewriteEngine On
RewriteBase /
RewriteRule ^file1\.html$ http://www.domain.com/file1.php [R=301,NC,L]
RewriteRule ^file2\.html$ http://www.domain.com/file2.php [R=301,NC,L]
RewriteRule ^file3\.html$ http://www.domain.com/file3.php [R=301,NC,L]
RewriteRule ^file4\.html$ http://www.domain.com/file4.php [R=301,NC,L]
A php header rewrite will not work as the old files are html type.

I suppose you could use some regex to reduce the number of different RewriteRules you are using, as those are all looking the same way.
In your case, using only this one might be OK :
RewriteRule ^(file1|file2|file3|file4)\.html$ http://www.metaboforte.com/$1.php [R=301,NC,L]
This way, you specify exactly what you want to rewrite ; but only have 1 RewriteRule.
Or, a bit more generic :
RewriteRule ^file([0-9]*)\.html$ http://www.metaboforte.com/file$1.php [R=301,NC,L]
Which allows you to define that you want to rewrite every fileXYZ.html, with XYZ a number. (As I used '*', no number at all would be taken into account by that rewrite rule ; if you want at least one number, you should use '+')
You could also do something even more generic -- not sure you want that, but something like this might do :
RewriteRule ^(.*?)\.html$ http://www.metaboforte.com/$1.php [R=301,NC,L]
Here, you are redirecting everything that end with .html

Why not just...
RewriteRule ^file([0-9]+)\.html$ http://www.metaboforte.com/file$1.php [R=301,NC,L]
Or, if you want to rewrite everything on the old site...
RewriteRule ^(.*?)\.html$ http://www.metaboforte.com/$1.php [R=301,NC,L]

Maybe you can try the RewriteMap Directive

Related

htaccess 3 different urls in one php file

I have 3 step in one php file:
This is my htacess now:
RewriteRule ^igra/(.*)$ "/index.php?page=igra&id=$1"
RewriteRule ^igra/(.*)/sezona/(.*)$ "/index.php?page=igra&id=$1&season=$2"
RewriteRule ^igra/(.*)/sezona/(.*)/liga/(.*)$ "/index.php?page=igra&id=$1&season=$2&league=$3"
When i go in browser something like index.php?page=igra&id=$1 or index.php?page=igra&id=$1&season=$2 or index.php?page=igra&id=$1&season=$2&league=$3 sure with real values it works fine, but when i try to access with this pretty links it always show me the first rewrite rule..
I hope u understand me what i need here, best regards..
Your first rule is capturing everything, so the subsequent rules never get executed. Just switch them around:
RewriteRule ^igra/(.*)/sezona/(.*)/liga/(.*)$ /index.php?page=igra&id=$1&season=$2&league=$3 [L]
RewriteRule ^igra/(.*)/sezona/(.*)$ /index.php?page=igra&id=$1&season=$2 [L]
RewriteRule ^igra/(.*)$ /index.php?page=igra&id=$1 [L]
Notice also the addition of the L flag.

Trouble mapping multiple subfolders to destinations in htaccess

We've switched servers and for whatever reason, our htaccess file isn't behaving the same way as it was on the other. I'm going to be the first to admit that I'm not an htaccess superuser, and I have no doubt the answer's probably looking me in the face, but literally an entire Saturday of searches hasn't fixed this. I have this filesystem:
When the domain is /category/subcategory/product/
It should rewrite to /category/product-details.php?p=product&s=subcategory
When the domain is /category/subcategory/
It should rewrite to /category-product-list.php?slug=subcategory
This seems simple enough, here's the same code we had been using for years. Note, we've commented out the first two RewriteRules regarding slashes and there was no change in behavior.
RewriteEngine On
RewriteBase /
# if folder does not end with a slash redirect to with slash
RewriteRule ^([-a-zA-Z0-9]+)$ /$1/ [L,NC,R=301]
#if it does not end with a slash e.g. rock-jewelry/some-piece, add the slash
RewriteRule ^([-a-zA-Z0-9]+/[-a-zA-Z0-9]+)$ /$1/ [L,NC,R=301]
RewriteRule ^category/([^/]+)/([^/]+)/?$ category/product-details.php?p=$2s=$1 [L,QSA]
RewriteRule ^category/([^/]+)/?$ category-product-list.php?slug=$1 [L,QSA]
When the domain is /category/subcategory/product/
It rewrites to /category-product-list.php?slug=product-details.php&p=product&s=subcategory
When the domain is /category/subcategory/
It correctly rewrites to /category-product-list.php?slug=subcategory
/category/ is an actual folder, and the only thing in it is product-details.php, there is no index.php file, the htaccess is supposed to rewrite to category-product-list if they're trying to access the index.
If we remove the category-product-list.php rule, the product-details.php rule DOES work. But isn't the L directive supposed to stop at the first rule? Why is the second still running? And how can I write a better way to accomplish this goal? Thank you very much, I'm pretty beat down on this problem at this point.
I have the same problem as you to understand the why of this loop... But it's like that.
You can add that before the last RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
Or if you don't use dot in subcategory, you can use final RewriteRule:
RewriteRule ^category/([^./]+)/?$ category-product-list.php?slug=$1 [L,QSA]

Htaccess rewrite with question mark

I have this type of link :
http://www.domain.it/index.php?module=Test&func=Prova
I would that when i add /en before /index.php
rewrite with
http://www.domain.it/index.php?module=Test&func=Prova&lang=en
I tried several methods but always rewrite index.php&lang=en
The problem seems to be ?
RewriteRule ^(en)/(.*)$ $2&lang=$1 [L,QSA]
but nothing to do.
You can try adding those lines to your .htaccess file:
RewriteEngine On
RewriteRule ^([a-z]{2})/(.*)$ $2?%{QUERY_STRING}&lang=$1 [L]
This will save the language in $1 and the origin in $2 so it can be used from other pages, besides index.php and with other languages besides en.
Also, to test those things, you can use sites like this one.

Apaches mod_rewrite issues

I'm having issues with apaches mod_rewrite. I'm wanting to make clean urls with my php application but it doesn't seem to give the results i'm expecting.
I'm using this code in my .htaccess file:
RewriteEngine on
RewriteRule ^project/([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
RewriteRule ^project/([0-9]{4})$ /project/index.php?q=$1 [L]
To make it so when I view, http://localhost/user/project/system, it would be the equivelant of viewing http://localhost/user/project/index.php?q=system
Instead of getting any results I just get a typical 404 error.
I've also just checked to see if mod_rewrite works by replace my .htaccess code with this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) http://www.stackoverflow.com
And it properly redirects me here, so mod_rewrite is definitely working.
The root path to my project is /home/user/public_html/project
The the url used to view my project is http://localhost/user/project
If anymore information is required let me know.
Thanks
If your .htaccess file is indeed located in the project/ subdirectory already, then don't mention it in the RewriteRule again. Remove it:
RewriteRule ^([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
# no "project/" here
Rules always pertain to the current local filename mapping.
Else experiment with a RewriteBase.
You have [0-9]{4} in your regex which will only match numbers of 4 digits. "system", however, is not a number of 4 digits, and therefore does not match.
You can use something like [^/]+ instead.
RewriteRule ([^/]+)/([0-9]{2})$ /index.php?q=$1&r=$2 [L]
RewriteRule ([^/]+)$ /index.php?q=$1 [L]
Don't know if the second parameter should be a number with 2 digits or not.
Edit: I also added "user" at the beginning now.
Edit2: Okay, I thought you were in the root htdocs with your htaccess. So remove "project" and "user" if you are in "project" with the .htaccess.
You probably mean
RewriteRule ^/project/([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
RewriteRule ^/project/([0-9]{4})$ /project/index.php?q=$1 [L]
The '^project' means "start of line is 'project'" but the start is a '/project', so you need to include the starting slash (i.e. '^/project...').
Sorry, missed the system bit (and the user bit). Was concentrating on the slash.
RewriteRule ^/user/project/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)$ /user/project/index.php?q=$1&r=$2 [L]
RewriteRule ^/user/project/([a-zA-Z0-9]*)$ /user/project/index.php?q=$1 [L]
Should have you right.

URL rewriting-code for rewriting

i am working for a site,which is in php.....i want to rewrite url
e.g www.3idiots.co.in/stories.php?id=17
if i want to rewrite it as
www.3idiots.co.in/stories/17.html
can any one tell me the code for this to write in .htaccess file.?
I'm assuming you're using Apache with mod_rewrite. Something like
RewriteEngine On
RewriteRule ^/stories/([0-9]+)\.html /stories.php?id=$1
should do the trick. Of course you'll need to make sure that RewriteRule is allowed in that directory. See this wiki page for more information.
mod_rewrite can only rewrite/redirect requested URIs and not those that are in your HTML documents. So you should first make sure, that your PHP application is printing the correct URIs, so /stories/17.html instead of /stories.php?id=17.
After that, you can use the rule suggested by José Basilio:
RewriteRule ^stories/([0-9]+)\.html$ stories.php?id=$1
Though redirecting requests of /stories.php?id=17 externally to /stories/17.html and then internally back to /stories.php?id=17 is possible, it’s not good practice as that would result in twice as many requests. But here’s the rule for that:
RewriteCond %{THE_REQUEST} ^GET\ /stories\.php[?\s]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&*([^&].*)?$
RewriteRule ^stories\.php$ /stories/%3.html?%1%4 [L,R=301]

Categories