Making a 301 rewrite from within a folder using relative rewrite URLs - .htaccess

I have the following problem. We used many different URLs to the same page. Now we want to use only one URL for those pages. Here an example:
RewriteRule ^(subfolder1/folder1/|(subfolder2|subfolder3)/folder2/|folder3/)?(name1|name2|name3|name4)$ scriptname.php [QSA,NC]
As you can see it is pretty messy. What I now want to do is the following: Rewrite all URLs to only one of those URLs (e.g. subfolder1/folder1/name1) using a 301 and than using a rewrite on that URL to address the actual script. It might look like this:
RewriteRule ^((subfolder2|subfolder3)/folder2/|folder3/)?(name2|name3|name4)$ /subfolder1/folder1/name1 [R=301,QSA,NC]
RewriteRule ^subfolder1/folder1/name1$ script.php [QSA.NC]
Until here I have no problem. But now comes the tricky part. We use several development machines on UNIX and Windows machines. They all have different host names and folder. Here are some examples:
http://www.example.com (production)
http://test.example.com (testing)
http://localhost/development_folder/ (development WIN)
http://localhost:8888/development_folder/ (development MAC)
The issue is, that as we have subfolders on the development machines, I can't use an absolute URL as /subfolder1/folder1/name1/ as it would e.g. point to http://localhost/subfolder1/folder1/name1/ and not to http://localhost/development_folder/subfolder1/folder1/name1/ so all the rewrites would be broken on the development machines.
Is there any chance to get this issue working? As the folder development_folderis the same on all development machines, would it help to exclude/include that folder to the rewrites afterwards like this:
RewriteRule ^((subfolder2|subfolder3)/folder2/|folder3/)?(name2|name3|name4)$ /development_folder/subfolder1/folder1/name1 [R=301,QSA,NC]
RewriteRule ^development_folder/(.*)$ $1 [QSA.NC]
RewriteRule ^subfolder1/folder1/name1$ script.php [QSA.NC]
Or is there a better way of doing it? Any hint would help a lot.

I would use Apache Include directives to modify the rule set. That way I can test the rules that will be used in production.
I don't remember if there is an issue with resolving the include wildcard to include no files, but if there is you could use an empty file in production and the actual dev rewrite rules one in dev.
RewriteRule ^((subfolder2|subfolder3)/folder2/|folder3/)?(name2|name3|name4)$ /subfolder1/folder1/name1 [R=301,QSA,NC]
Include /etc/apache/conf.d/*_dev.rewrites
RewriteRule ^subfolder1/folder1/name1$ script.php [QSA.NC]
On you dev machines and ONLY on your dev machine have the /etc/apache/conf.d/my_dev.rewrites:
RewriteRule ^development_folder/(.*)$ $1 [QSA.NC]

Wouldn't per-directory RewriteRules (inside an .htaccess) work? Then you can specify either a relative substitution (without a / in front) or, in case you need it, an absolute one (with /).
With the relative one, the base path will be stripped before rewriting and then prefixed again. Perhaps, you'll need to tune RewriteBase for this to work correctly (hmm, differently for your different servers...).

I think there is a more deep problem with that dev/production servers layout but I suppose you have your reasons.
This can help you:
# dev
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteRule ^((subfolder2|subfolder3)/folder2/|folder3/)?(name2|name3|name4)$ /subfolder1/folder1/name1 [R=301,QSA,NC]
RewriteRule ^subfolder1/folder1/name1$ script.php [QSA.NC]
# production
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^((subfolder2|subfolder3)/folder2/|folder3/)?(name2|name3|name4)$ /dev_folder/subfolder1/folder1/name1 [R=301,QSA,NC]
RewriteRule ^dev_folder/subfolder1/folder1/name1$ script.php?dev=1 [QSA.NC]
The idea is to use the hostname to have a different set of rules. You can use dev1.example.com, dev2.example.com, etc as needed adding that 'fake' domains to /etc/hosts as alias of 127.0.0.1

Related

how to rewrite SEO URL

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/files/section.php?id=1
Changed to:
demo.example.com/sample-section
I tried
Since you use .htaccess I assume you are using Apache. Here you'll find all relevant documentation.
First of all you need the mod_rewrite module to be installed (instructions to do so depend on the server's operating system and Apache distribution).
Then, the URL rewrite is pretty simple:
# First of all tell to mod_rewrite to operate.
RewriteEngine on
# Then, as many times you need, tell it on what to operate...
# For example: on files that do not exist. Or leave out RewriteCond to act on all.
RewriteCond "%{REQUEST_FILENAME}" !-f
# ...and what to do.
RewriteRule /sample-section "/files/section.php?id=1" [PT]
RewriteRule /another-section "/files/section.php?id=2" [PT]
The PT (PassThru) flag might be needed in some contexts, otherwise just use [L].

Using mod_rewrite to redirect .dev domains

In the Apache 2.4 docs on dynamic virtual hosts, it says:
Mass virtual hosts with mod_rewrite
Mass virtual hosting may also be accomplished using mod_rewrite, either using simple RewriteRule directives, or using more complicated techniques such as storing the vhost definitions externally and accessing them via RewriteMap. These techniques are discussed in the rewrite documentation.
I'm attempting to use mod_rewrite instead of mod_vhost_alias because I want it both ways: localhost/project and project.dev should point to the same folder, but either URL should work.
Here's my latest attempt (currently in an .htaccess), which gets me a lovely 500 error.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
If I do
...
RewriteRule ^(.*)$ http://localhost/%1/$1 [L,QSA]
I can access the files, but the URL changes (not what I want). I've tried a variety of permutations with and without slashes, RewriteBase, etc.
To be clear, I want project.dev/index.php and localhost/project/index.php to both be valid non-redirected references to /var/www/html/project/index.php. And I'd like to do this in a dynamic way, so I don't need to enter a new set of rules for every folder.
I'm not fixated on doing this with .htaccess - virtualhosts are ok too as long as they're dynamic and I can still access my sites using the localhost/ scheme and the other machines on the network can connect to the sample sites in the usual way (192.168.1.22/project/index.php).
Try this rule:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(.+)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

Rewrite URL and remove part with .htaccess

I am really stuck with my .htaccess file, and need some help :). I have a WordPress installation that I am using for testing. It is in a folder and I use .htaccess to get there. This is the rules I use so far:
######### Custom #########
RewriteEngine On
# ignore folders
RewriteCond %{REQUEST_URI} "/af1wp/"
RewriteRule (.*) $1 [L]
###############
# only for me #
###############
# HOME (Senne Tijdeman)
RewriteCond %{REMOTE_ADDR} ^###\.###\.###\.###$
RewriteCond %{HTTP_HOST} ^((www.)?([a-z0-9_\-]+).)?alleenf1.nl$
RewriteCond %{REQUEST_URI} !^/af1wp/$
RewriteRule ^(.*)$ /af1wp/$1 [L]
This works (with my real IP address of course), so no problem there. But now I want to rewrite exisiting URL's to a new format. The old URL is this:
http://alleenf1.nl/nieuws/QOgbb/raikkonen-alles-is-mogelijk-in-australi
The new URL should be this:
http://alleenf1.nl/raikkonen-alles-is-mogelijk-in-australi
The part I want to remove "nieuws/QOgbb/" is not always the same, so I have to use regex for that. But everything I tried did not work at all.
I thought this would be simple, but apparently not for me unfortunately. Now I have 2 questions.
What is the right RewriteRule to do this?
Where should I put it. In the .htaccess of the root folder, or the af1wp folder where the WordPress install is?
Tnx in advanced
To awnser the questions from poncha below:
Yes, the URL's always start with to folders. Just to clarify (was not clear) the part "nieuws" is always the same, but not the second part (call it an ID).
I prefer a redirect.
The file /raikkonen-alles-is-mogelijk-in-australi is a post in WordPress. That WordPress installation currently resides in the folder af1wp, but will be moved to the root folder when going live.
Try this:
RewriteEngine On
RewriteRule ^nieuws/([^/]+)/(.*) /af1wp/$1 [R=301,L,QSA]
This will only match URLs starting with "nieuws"
For now, the rewrite target is /af1wp/, change it to / when moving the wordpress.
When you move wordpress, you'll need to mix in this rule inside the wordpress rules, as it already has rewrite rules of its own - place this rule above its rules.
The flags used here:
R=301 - redirect with HTTP status 301 (Moved Permanently).
L - last rule (stop rules parsing after successful match of this rule)
QSA - query-string-append (append original query string to the rewritten request).

htaccess subdomain rewrite without a redirect

Using htaccess Rewrite, I want my url http://*.phoneataxi.com/ (where * is a wildcard, excluding 'www') to show in the address bar as is but get information from http://*.phoneataxi.com/test.php?c=*.
I have tried so many different things but nothing is doing exactly what I need. Most examples are redirecting the subdomain to the '/test.php' file in the address bar which I don't want to do.
I'm trying not to have to create individial subdomains and subdomain folders within my webroot.
Ideas?
I use this htaccess file to make Apache act as a proxy for another host:
IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ghost\.pileborg\.se$
RewriteRule (.*) http://vps.pileborg.se/ghost/$1 [P]
</IfModule>
It causes all access to http://ghost.pileborg.se/ to be "redirected" to http://vps.pileborg.se/ghost/.
UPDATE (2020)
Some of the answers regarding this topic is very old and no longer work as expected.
After searching for hours on something that actually works, this is what I came up with; edit as you see fit:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ([a-z0-9]+)\.
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.php -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.html -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.html [L,NC,QSA]
Breakdown
Make sure that the rewrite module is installed and enabled on your host
first we turn the rewrite engine on and set the path-base
then isolate the subdomain - any letters/numbers before the first dot
set a variable in this runtime environment that contains the subdomain
check if the subdomain folder and index-file exists
if it does exist -then use that file as the request-handler (no redirect)
if it does not exist then the request carries on normally
Flags
The flags used here are explained here, but the ones used above are quite simple:
[L] Last rule, ignore the rest
[NC] No Case, no uppercase/lowercase restrictions
[QSA] I remember this as "Query String Attach" :D

Subdomain alias with htaccess

I have this code for managing a subdomain alias: browsing sub.domain.com it shows domain.com/fp/sub.
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
RewriteRule (.*) /fp/sub/$1
I have a lot of these subdomain to setup. Is there a way to automate this process placing some variables instead of the correct subdomain/folder name?
I tried this code with no success
RewriteCond %{HTTP_HOST} ^(sub1|sub2)\.domain\.com$
RewriteRule (.*) /fp/$1/$2
Francesco, first correcting your example, the rule should be:
RewriteRule ^(.*) fp/%1/$1 [L]
The % variables are set in the last successful cond regexp match.
Second there are lots of options if you have access to the system or vhost config, such as using Rewrite Maps or mass virtual hosts. If you don't have such access are limited to .htaccess, then you need to use one or more rules much as you are already doing. One variant is to use an existence check of the target directory, such as:
RewriteCond %{HTTP_HOST} ^(\w+)\.domain\.com$
RewriteCond %{DOCUMENT_ROOT}/fp/%1 -d
RewriteRule (.*) fp/%1/$1 [L]
Beware that if you are using a shared hosting service, that DOCUMENT_ROOT may not be properly set at rewrite execution, in which case your SHS provider will set up an environment variable to do the same, e.g. %{ENV:DOCUMENT_ROOT_REAL}. Do a phpinfo to find out. See Tips for debugging .htaccess rewrite rules for more advice on how to debug this.

Resources