mod_rewrtie remap and redirect - mod-rewrite

what i am trying to do is this:
If url http://example.com/foo/foo.php is called it should be redirected with 301 to http://example.com/bar/foo.php
AND
if url http://example.com/bar/foo.php is called it internally calles the /bar/foo.php script but browser url is not changed (remap).
my rules look like this
RewriteRule ^foo/foo.php(.*) bar/foo.php$1 [R=301]
RewriteRule ^bar/foo.php(.*)$ foo/foo.php$1 [PT]
But this gives me too many redirects error.
Each rule activated separately works but together they seem to conflict...

You can either match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo/foo.php
RewriteRule ^foo/foo.php(.*) bar/foo.php$1 [R=301]
RewriteRule ^bar/foo.php(.*)$ foo/foo.php$1 [L]
Or prevent rewrite looping altogether:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^foo/foo.php(.*) bar/foo.php$1 [R=301]
RewriteRule ^bar/foo.php(.*)$ foo/foo.php$1 [L]

Related

"If request" in .htaccess?

I use rewrite condition to redirect website always to www. my code is:
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
I can't found on the internet how can i do request if in url '.com' i need it because the website must be also localy accessible.
For example: i found this, but i can't understand how can i implement it with my script.
Your current RewriteCond is correct for applying www. to website.com if it is not already present. To avoid the RewriteRule happening when working on localhost, you need an additional RewriteCond to check the host.
This is because the condition !^www\.example\.com$ matches any domain except www.example.com, which includes localhost.
# Only apply other conditions if not working on localhost
RewriteCond %{HTTP_HOST} !localhost [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
You can make it a little more dynamic in case you need to work with multiple domains (example.com, example.org) and transform each to www.example.com, www.example.org:
# If not on localhost
RewriteCond %{HTTP_HOST} !localhost [NC]
# and the domain does not begin www.
RewriteCond %{HTTP_HOST} !^www\.
# Redirect to apply the www. to HTTP_HOST
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
Use caution when testing - your browser may cache old 301 redirects. You may need to change them to R=302 during testing and clear your browser cache. When you are satisfied it works, change it back to R=301.
That RewriteCond should already work for your case, the next rule is processed only if the condition matches, so that rule is not processed for localhost.

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

apache mod rewrite with the_request

I want to do the following with apache (mod rewrite).
if the user requests http://hostname.tld/index.php/folder/subfolder i want it to redirect (with a R=301) to http://hostname.tld/folder/subfolder.
if the user requests http://hostname.tld/folder/subfolder the request should internally be rewritten to index.php/folder/subfolder.
To prevent an endless redirect the first rule should check for %{THE_REQUEST}. The problem here is that I am unable to append "folder/subfolder" with a regex. How should I do this?
For the second rule I have this (and seems to work).
RewriteCond %{HTTP_HOST} hostname.tld [NC]
RewriteRule ^(.*)$ index.php [QSA,L]
The first one is still a problem.
I think the first one should be something like
RewriteCond %{THE_REQUEST} (.*)index.php(.*) [NC]
RewriteRule /index.php/$ http://hostname.tld/$1 [R=301,QSA,L]
But that is not really it.
The first should be.
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteCond %{THE_REQUEST} index\.php [NC]
RewriteRule ^index.php/(.*)$ http://hostname.tld/$1 [R=301,L]
I also see that your second rule redirects http://hostname.tld/folder/subfolder to http://hostname.tld/index.php (not http://hostname.tld/index.php/folder/subfolder). But as long as that works it's fine, as this it also prevents the redirect loop.
But just in case, here is the solution to add the folder/subfolder part:
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

How to redirect an url with parameters?

I'm at a total loss trying to integrate a mod_rewrite in my existing page. Currently, I am building a new site from scratch, and there i have some nice clean url's such as:
http://www.example.nl/nl/example
The old site, running Cms made simple, has some not-rewritten url's that would need to be redirected to the new pages. Those url's look like this:
http://www.example.nl/index.php?page=cake-and-pie&hl=nl_NL
But shorter versions of that like:
http://www.example.nl/index.php?page=cake-and-pie
also work.
It took me a while to figure out that url's with parameters cannot simply be redirected with "Redirect 301", like i'd normaly do. So i tried some online mod_rewrite generators like this and this, but the rules outputted by those result only in 404 errors, (the redirect doesn't work at all).
My .htaccess file current looks like this:
RewriteEngine On
RewriteBase /
# remove .php;
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
The old pages would seize to exist.
How do i redirect the old pages to the new ones?
Thanks.
EDIT
RewriteCond %{QUERY_STRING} =page=pie
RewriteRule ^index\.php$ /nl/? [L,R=301]
RewriteCond %{QUERY_STRING} =page=pie&hl=nl_NL
RewriteRule ^index\.php$ /nl/? [L,R=301]
Seems to do the trick. This is of course manual for every url, but i only have a few.
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/([^/]+)/?$ index.php?page=$1&hl=$2
RewriteRule ^([^/]+)/?$ index.php?page=$1 [QSA]

How mod_rewrite can add subdomain?

RewriteEngine On
RewriteRule ^/ai?$ /Market/publish.jsp [QSA,L,PT]
RewriteRule ^/ar?$ /Market/MailDispatch [QSA,L,PT]
RewriteCond %{HTTP_HOST} !^web\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://web.example.com/$1 [L,R]
#How skip www\. to web\. for this 1 ?
#RewriteRule ^/vi/?([0-9]+)\.htm$ /Market/vi.do?id=$1 [PT,L]
RewriteRule ^/li /Market/list.do [QSA,PT,L]
RewriteRule ^/vi/locations.jsp /Market/locations.jsp [PT,L]
ErrorDocument 404 /notfound.html
Nearly undoable(?) I try http://example.com/vi/{N}.htm should redirect to http://web.example.com/vi/{N}.htm where N is dynamic ID.
Seen mod_rewrite with subdomain and url pattern
There is no clear way to make eg http://example.com/vi/1096.htm pass up to next version http://web.example.com/vi/1096.htm where number is dynamic. I tried
A rule with the following scheme should do it:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/vi/\d+\.htm$ http://web.example.com%{REQUEST_URI} [L,R=301]
It’s important to put this rule in front of those rules that do an internal redirect. Otherwise an already internally rewritten URL could be rewritten externally.
If you want to use this rule in a .htaccess file, remove the leading slash from the pattern in RewriteRule.

Resources