I have this code to rewrite a path, and then redirect to subdomain's folder:
RewriteEngine On
RewriteBase /
RewriteRule ^category/?$ category.php
RewriteRule ^category/[a-z0-9\-]+\_([A-Z0-9\_]+)/?$ articles.php?id=$1§ion=category
RewriteCond %{HTTP_HOST} ^test.(.*)example.com/?$ [NC]
RewriteCond %{REQUEST_URI} !^/\!test/ [NC]
RewriteRule (.*) /!test/$1 [L,NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [NE,L,R=301]
The big problem is that typing this in the browser address field and press Enter:
http://test.example.com/category/article-about-something_35436
Ends up in that it changes by itself to:
http://test.example.comcategory/article-about-something_35436
And the browser of course tells you that "Server is not found".
Troubleshooting
I have figured out that this works fine:
http://test.example.com/category
And removing the query string ?id=$1§ion=category from the second rewrite rule works. You don't end up on the article of course but on articles.php.
So, anyone know the problem?
Rearrange your rules and keep redirect rules with R flag before other internal rewrite rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^category/?$ category.php [L,NC]
RewriteRule ^category/[a-z0-9-]+_([A-Z0-9_]+)/?$ articles.php?id=$1§ion=category [L,QSA]
RewriteCond %{HTTP_HOST} ^test\.(.*)example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/\!test/ [NC]
RewriteRule (.*) /!test/$1 [L,NC]
Related
I have the following .htaccess, which is not working as expected:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule (.*) http://www.example.bg/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} ^/login.* [OR]
RewriteCond %{REQUEST_URI} ^/admin.*$
RewriteRule ^(.*)$ travelpartner.php [L,NC]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} !^/login.*$
RewriteCond %{REQUEST_URI} !^/admin.*$
RewriteRule ^(.*)$ perla/$1 [L,NC]
What I need is the following:
If the URI is missing the "www" from example.bg, then add it and redirect to www.example.bg
If the URI does not end in "sessiontest.php" and starts with "www.example.bg/login" or starts with www.example.bg/admin, then forward all requets to travelpartner.php
For all other cases, we should forward all requests to a subfolder, called "perla"
However, right now what happens is that we always see the contents of the perla subfolder, even if I open exemple.bg/login
What am I missing here?
With your current rules I get the infinite redirect loop:
Once the rule No.2 is matched, and the request /login is rewritten to /travelpartner.php, the rewritten request is handed back to the URL parsing engine and the ruleset is run again from the start.
This time, it will match the last segment and be rewritten to /perla/travelpartner.php,and sent back again, and be rewritten to /perla/perla/travelpartner.php, etc...
The fix is to, in the last segment, prevent rewriting if the request starts with /perla or is /travelpartner.php:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule (.*) http://www.example.bg/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} ^/login [OR]
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^ travelpartner.php [L,NC]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} !^/login
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^/perla/
RewriteCond %{REQUEST_URI} !^/travelpartner\.php$
RewriteRule ^(.*)$ perla/$1 [L,NC]
You can use the following rules
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteRule ^(login|admin) /travelpartner.php [L]
RewriteRule !(perla/|travelpartner\.php) /perla [L]
Here is my htaccess code:
RewriteEngine On
RewriteRule ^s/(.*)/(.*) /index.php?search=$1&category=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
I need to make a rewrite rule like this:
http://mywebsite.com/s/query+term => http://mywebsite.com/?search=query+term
OR if there is a category
http://mywebsite.com/s/query+term/Category => http://mywebsite.com/?search=query+term&category=Category
I need also to redirect all the old urls to the new one.
With this rules all that I can obtain is to have the search term and category name joined togheter. In a nutshell it is as if I had always:
http://mywebsite.com/?search=query+term/Category
If I remove all the conditions (RewriteCond), leaving only the rewrite rule with the addition of a trailing slash:
RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
Reaching the url of my interest in a direct way, the whole thing works. But I will not have the redirect..
So I have tried to set a rule like this:
RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
But with this rule I get just a loop of redirects.
You can use the following rules :
RewriteEngine on
#1--Redirect from "?search=foobar&cat=cat" to "/s/foobar/cat" --#
RewriteCond %{THE_REQUEST} /\?search=([^&]+)&cat=([^\s]+) [NC]
RewriteRule ^ /s/%1/%/%2? [NC,L,R]
#2--Redirect from "/?search=foobar" to "/s/foobar" --#
RewriteCond %{THE_REQUEST} /\?search=([^/\s]+) [NC]
RewriteRule ^ /s/%1? [NC,L,R]
#1--Rewrite "s/foobar/cat" to "/?search=foobar&cat=cat"--#
RewriteRule ^s/([^/]+)/([^/]+)/?$ /?search=$1&$category=$2 [NC,L]
#2--Rewrite "s/foobar" to "/?search=foobar"--#
RewriteRule ^s/([^/]+)/?$ /?search=$1 [NC,L]
I have following mod_rewrite rule to redirect from site.com to www.site.com:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=permanent,L] .
I need to exclude from this rule urls starting with /yandex_market
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{REQUEST_URI} !^/yandex_market.*$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=permanent,L] .
But rule still works on urls starting with /yandex_market How to fix it?
Problem is that your 2nd rule rewrites /yandex_market/foo URI to /index.php?module=YandexPurchaseView&type=foo and thus making RewriteCond %{REQUEST_URI} !^/yandex_market.*$ [NC] condition succeed. You will need to use %{THE_REQUEST} variable for your condition which doesn't change with application of rewrite rules.
Keep your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{THE_REQUEST} !/yandex_market [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NE,L]
RewriteRule ^yandex_market/(.+)$ index.php?module=YandexPurchaseView&type=$1 [L,NC,QSA]
I'm trying to rewrite some legacy Joomla URLs on a site that's now using ExpressionEngine as its CMS but they're not working.
The ExpressionEngine URL rewrites, i.e. removing index.php from the URL work fine though.
This is what I've got:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This is the one that's not working
RewriteRule /index.php?option=com_chronocontact&Itemid=54 /contact/ [R=301,L]
# Force www
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Redirect index.php Requests
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
RewriteCond %{REQUEST_URI} ^/
RewriteCond %{QUERY_STRING} ^(gclid=.*)
RewriteRule ^(.+) /index.php?/ [L,PT]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(assets|css|images|tinymce|js|min|cms|themes|index\.php|admin\.php|favicon\.ico|index\.php|path\.php|php\.ini) [NC]
RewriteRule ^(.+) /index.php?/ [L]
</IfModule>
Can anyone spot what I'm doing wrong?
The first thing is the stray RewriteCond %{HTTPS} !=on that you have at the top. It looks like it belongs to the rule under it, as in:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
As far as the rule that you have commented that doesn't work, the ? is a reserved character for regular expressions, and your pattern actually says that the second p in /index.php is "optional". Additionally, you can't match against the query string in a rewrite rule, you need to use a rewrite condition and match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^option=com_chronocontact&Itemid=54$
RewriteRule ^(index.php)?$ /contact/? [R=301,L]
is probably more along the lines of what you're looking for.
I am running wildcard subdomains. the aim of the site is to run a virtual subdomain. what the functionality should be is that when the files
from the main site are called they will be rewritten for the clean URLs. But when i am calling the same files form the subdomain
like xyz.doamin.com/my-file.php then this should be rewritten like it is calling that file with an argument of subdoamin like
domain.com/my-file.php?var=xyz. Currently what i have done for the file call is this
Options +FollowSymLinks
RewriteEngine on
## Redirecting all non-www domains to www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?var=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&var=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com/index.php?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&subdomain=%1 [L]
This is throwing a 500 internal error for the file call.
Rule set 4 will overlap with rule set 1 and 2. I suspect this is causing the issue.
So I think all 3 need an extra RewriteCond check on REQUEST_URI.
Really you shouldn't need rule set 4. Just including index.php as a default should do the trick. Unless of course you don't want URL's like http://www.domain.com/?var=xyz, in this case you need rule 4, but you need to split it into 2 - one with a QUERY_STRING and one without, just like rule 1 and 2
Also, isn't %1 from the regex in the rewriteRule itself and not the preceeding rewriteConds? If so then your regex is wrong, because it'll be setting the var= to the entire original URL.
Looking in the error log is a good idea as it will give you the reason for the 500 error.
Edit: Ok, try something like this (not tested)
## Redirecting all non-www domains to www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L,QSA]
## Redirecting / requests to index.php
# Note: Use the QSA flag for handling with or without query string
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/index.php?var=%1 [L,QSA]
## Redirecting /something requests to index.php
# Note: Use the QSA flag for handling with or without query string
RewriteCond %{REQUEST_URI} ^/.+$
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?var=%1 [L,QSA]