How can I set the default index page to first.html when there's no query string present?
www.myexample.com/?pg=456 -- should use index.html
www.myexample.com -- should use first.html
You can use in your .htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} !pg=.+ [NC]
RewriteRule ^$ first.html [NC,L]
You can use the following rule :
RewriteEngine on
#when there are query strings#
RewriteCond %{QUERY_STRING} .+
#Rewrite "/" to "index.html"#
RewriteRule ^$ /index.html? [NC,L]
#when there are no query strings#
RewriteCond %{QUERY_STRING} ^$
#Rewrite "/" to "first.html"#
RewriteRule ^$ /first.html [NC,L]
Related
I redirect all virtual subdomains as follows.
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$ subdomain.php?url=%1 [NC,QSA,L]
This way, I get data from the database. All "virtual subdomains". But there is a real subdomain. Opencart inside (shop.mysite.com)
htaccess directs this as well. How do I prevent it from just redirecting the word "shop".
My Full Code:
Options Indexes SymLinksIfOwnerMatch
RewriteEngine on
directoryindex index.php
Options -indexes
RewriteRule index index.php
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$ subdomain.php?url=%1 [NC,QSA,L]
# www.site.com >> site.com
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$ subdomain.php?url=%1 [NC,QSA,L]
You can add another condition to exclude the shop subdomain. For example:
RewriteCond %{HTTP_HOST} !^shop\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1 [QSA,L]
The ! prefix negates the regex, so the condition is successful when it does not match.
OR, you could use a negative lookahead in your original condition regex. For example:
RewriteCond %{HTTP_HOST} ^((?!shop\.).+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1 [QSA,L]
However, your directives are also in the wrong order. Your external redirect to remove the www subdomain should appear first, otherwise you need to incorporate another exception into the above rule.
For example:
Options SymLinksIfOwnerMatch
DirectoryIndex index.php
RewriteEngine on
RewriteRule index index.php
# www.site.com >> site.com
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^shop\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1 [QSA,L]
You were also adding the Indexes option, only to remove it again with a later directive.
Let's say we have an url like this:
http://domain.com/?theme_of_site=parameter_value
http://domain.com/?type_of_site=parameter_value
domain.com - stable/static, always the same
theme_of_site & type_of_site - stable/static, the same when choosen
parameter_value - dynamic
It's like to have always the url's like:
http://domain.com/?theme=parameter_value
http://domain.com/?type=parameter_value
How can I write this in .htaccess?
For the below discussion:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Start by redirecting the original links from type_of_site= to type=
# Only if the browser's original request contains the strings
RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteCond %{THE_REQUEST} type_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1&type=%2 [L,R=301]
RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1 [L,R=301]
RewriteCond %{THE_REQUEST} type_of_site=([^&]+)
RewriteRule (.*) $1?type=%1 [L,R=301]
# Then, after performing the initial redirects to change the browser
# URL, rewrite the parameters internally
# Match both present:
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1&type_of_site=%2 [L]
# Then match each individually
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1 [L]
RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?type_of_site=%1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Query string parameters need to be captured in RewriteCond. You can match (theme|type) and capture the value, then rewrite it internally using the (theme|type)_of_site in a RewriteRule.
RewriteEngine On
# Match the theme or type paramater in `%1` and capture its value in `%2`
RewriteCond %{QUERY_STRING} (theme|type)=([^&]+)
# Rewrite The parameter name to include _of_site using the values captured above
# for all URLs as captured in $1
RewriteRule (.*) $1?%1_of_site=%2 [L]
The simple example above works only if either of the parameters is specified. If you need to be able to handle both at once like example.com/?type=abc&theme=123 it gets a little more complex:
# Start by redirecting the original links from type_of_site= to type=
# Only if the browser's original request contains the strings
RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteCond %{THE_REQUEST} type_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1&type=%2 [L,R=301]
RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1 [L,R=301]
RewriteCond %{THE_REQUEST} type_of_site=([^&]+)
RewriteRule (.*) $1?type=%1 [L,R=301]
# Then, after performing the initial redirects to change the browser
# URL, rewrite the parameters internally
# Match both present:
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1&type_of_site=%2 [L]
# Then match each individually
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1 [L]
RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?type_of_site=%1 [L]
Let's say short.com is the short domain and long.com is the long domain
updated:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.long.com
RewriteRule (.*) http://long.com/ [R=301]
RewriteCond %{HTTP_HOST} ^short\.li$ [NC]
RewriteCond %{REQUEST_URI} !^/redirect
RewriteRule ^(.*)$ /redirect?short=$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)short\.li$
RewriteRule ^$ http://long.com/ [L,R=301]
both domains point to that root directory. When I type short.li I end up on long.com/?l=
how did I manage to screw up like that?^^
Try this in your htaccess file :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)short\.com$
RewriteRule ^$ http://www.long.com/ [L,QSA,R=301]
Remove RewriteEngine on if it is already there
I think you might want something along the lines of this:
RewriteEngine on
RewriteCond {REQUEST_URI} !/
RewriteCond {HTTP_HOST} short.com
RewriteRule ^(.*) http://long.com/redirect.php?short=$1 [L,R=301]
RewriteCond {REQUEST_URI} /
RewriteCond {HTTP_HOST} short.com
RewriteRule ^(.*) http://long.com/ [L,R=301]
Not sure if the regex is needed on that last one or not, but something like this should work
I am attempting to get the following results with an .htaccess file.
URL: Behavior(rewrite to):
example.com/x/ view-test.php?page=x
example.com/x/y view-test.php?page=y&parent=x
example.com/x/y/z view-test.php?page=z&parent=y&grandparent=z
I have the following .htaccess rules, which also include some folders which needs to ignore this scheme:
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
#RewriteRule ([^/]+)$ /view-test.php?page=$1 [L]
however, whenever I uncomment the last two lines I get an internal server error. the parent/page and grandparent/parent/page structures work fine. How can I re-write this last rule (and the preceding rules if nessecary) to achieve the desired result for the first pattern (just a page)?
# remove slash at the end to prevent redirect loop
RewriteCond %{REQUEST_URI} ^/(OIE|admin|images|scripts|css|fonts|view-test\.php) [NC]
RewriteRule ^ - [L]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /OIE/view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /OIE/view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]+)/?$ /OIE/view-test.php?page=$1 [L]
Use this code instead:
RewriteCond %{REQUEST_URI} ^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ^ - [L,S=3]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ view-test.php?grandparent=$1&parent=$2&page=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ view-test.php?&parent=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ view-test.php?page=$1 [L,QSA]
Problem was that you were missing trailing slash / in in your last RewriteRule condition.
Try this :
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ view-test.php?page=$1
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2&grandparent=$3
I have the following .htaccess at present.
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/abc/php.ini
</IfModule>
RewriteEngine on
RewriteCond $1 !^(index\.php|media|css|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
AuthType Basic
AuthName "Está dominado!!"
AuthUserFile "/home/abc/.htpasswds/public_html/passwd"
require valid-user
Now the thing is, I would like to, if any query string is found after a / remove that query string.
So:
If we have:
http://www.abc.com/?somethinghere234
It should redirect to:
http://www.abc.com/
If we have:
htpp://www.abc.com/something/?id212
Redirect to:
htpp://www.abc.com/something/
UPDATE:
I've tried this:
TRY A)
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* /index.php/ [L,QSA]
Hopping that ^(.*)$ will allow anything on the query string...
I've also tried this:
TRY B)
RewriteEngine on RewriteCond $1
!^(index.php|media|css|js|images|robots.txt)
RewriteCond %{QUERY_STRING} ^(.)$
RewriteRule ^(.)$ http://abc.com/ [L]
This returns a 500 internal server error.
TRY C)
RewriteEngine on
RewriteCond $1 !^(index\.php|media|css|js|images|robots\.txt)
#RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ http://abc.com/ [R=302,L]
I confess I'm kind of lost on this .htaccess sintax.
TRY D)
RewriteEngine on
RewriteCond $1 !^(index\.php|media|css|js|images|robots\.txt)
RewriteCond %{REQUEST_URI} !^/
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ http://abc.com/ [R=302,L]
Please advice,
MEM
The following should work:
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^ /index.php/? [L]
check if you've got something in QUERY_STRING (yours also accepts an empty string)
your check to prevent endless redirect
redirect to wherever you want.
From the Apache documentation:
When you want to erase an existing
query string, end the substitution
string with just a question mark
EDIT (again): As external redirect (you may not even need the second RewriteCond if you don't have any query string at all in your application):
This will tell the client to request the same URI without query string.
RewriteEngine on
# redirect to same URI, but without query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI}? [R=301]
# CodeIgniter "magic"
RewriteCond $1 !^(index\.php|media|css|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
I wasn't sure from your description exactly what you were after, so you should keep or remove the second RewriteCond depending on your specific needs.
RewriteEngine On
# Remove the query string with a redirect
RewriteCond %{QUERY_SRING} !=""
# But only if the path ended with a /
# (remove this to redirect anything with a query string)
RewriteCond %{REQUEST_URI} /$
RewriteRule .* /$0? [R=301,L]
# Then perform your internal rewrite
RewriteCond $1 !^(index\.php|media|css|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Possibly because you have QSA added on there - it means Query String Append.
RewriteCond %{REQUEST_URI} ^(.*/)([^/]+)$
RewriteRule . %1 [R=301,L]