I have below code in my .htaccess file. When I open https://example.com/tran it shows 404 page but adding slash in url like https://example.com/tran/ works.
How can I make https://example.com/tran to redirect to https://example.com/tran/ and https://example.com/tran/parameter1 both work because also want to get parameter from url using $_GET method when it is available in url.
Options All -Indexes
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
<FilesMatch ".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
Order Allow,Deny
Deny from all
</FilesMatch>
RewriteRule tran/(.*) transaction.php?id=$1
RewriteRule tran/(.*)/ transaction.php?id=$1
RewriteRule addr/(.*) address.php?id=$1
RewriteRule addr/(.*)/ address.php?id=$1
You can use:
RewriteRule tran(?:/(.*?)/?)?$ transaction.php?id=$1
RewriteRule addr(?:/(.*?)/?)?$ address.php?id=$1
instead of your 4 rules
Related
I've a small, but hard to understand problem with .htaccess in CMS system.
I've mod expires, that cache stuff on whole website, but I don't want to cache stuff in /admin URL, I can't make another .htacess, couse I've MVC structure and no real directory that could hold all my admin stuff.
I've found directive, but it only works in server configuration and I want it to work on different hostings, so only in htaccess file.
EDIT- Rewrite
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ![0-9]$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
You can apply your Expires directive using a <if> directive with an expression to match against /admin:
<If "%{REQUEST_URI} =~ /^\/admin\//">
# Your expiry directives
</If>
If you know the exact URL then you can try this pattern.
RewriteRule ^facebook/get/(.*)?$ http://$1 [NC,R]
RewriteRule ^wrapper/share/(.*)?$ http://example.com/wrapper/share/$1 [NC,R]
This will check for URL where <-any-value->facebook/get/<-any-value2-> and then will send to the <-any-value2->
Like
RewriteRule ^stats/(.*)$ admin/dashboard.php?mode=openstats&event_id=$1 [NC,L,QSA]
**If URL has stats/<--any-value--> then it will redirect/open admin/dashboard.php **
If your URLs doesn't have exact value but you do know the URL slot pattern then you can try this.
RewriteRule ^([^/.]+)/([a-zA-Z0-9_-]+)/$ wrapper/index.php?id=$2 [NC,L,QSA]
This seems simple enough, but the documentation and search results on mod_rewrite are a bit lacking. In the interest of saving time, can someone explain to me the best way to direct my traffic?
If the url is /show/checkout I want https://servername.com/show/checkout. If the url is anything else, I want it to go to http://servername.com/show/whatever.
I've got mod_rewrite enabled. I am able to send all traffic to https, but I haven't succesfully filtered out /show/checkout traffic. This doesn't work because there is some insecure images/scripts that are causing security warnings in some browsers.
Thanks!
Current .htaccess that sends all traffic to https:
Order allow,deny
Allow from all
Deny from 65.208.151.
Options -Indexes
AddHandler fastcgi-script .fcgi
AddDefaultCharset UTF-8
AddDefaultCharset ISO-8859-1
DirectoryIndex index.cgi
RewriteEngine on
RewriteRule ^/(.*)$ http://myserver.com/$1 [R,L]
RewriteRule ^show/product/(.*)$ ?content=product;title=$1 [QSA,L]
RewriteRule ^show/(.*)/(.*)$ ?content=$1;nth=$2 [QSA,L]
RewriteRule ^show/(.*)$ ?content=$1 [QSA,L]
RewriteRule ^place/order$ ?email=order [QSA,L]
RewriteRule .htm$ /
This rule:
RewriteRule ^/(.*)$ http://myserver.com/$1 [R,L]
doesn't do anything, since URI's used to match on won't start with a /. Not just that, this rule is simply a redirect loop.
Remove that and try adding this:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_REFERER} !show/checkout
RewriteCond $1 !^show/checkout
RewriteRule ^(.*)$ http://servname.com/$1 [L]
RewriteCond %{HTTPS} off
RewriteCond $1 ^show/checkout
RewriteRule ^(.*)$ https://servname.com/$1 [L]
I just want to append the url with home but it is happening in recursion and i get the browser saying too many redirects
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteRule ^test main.html
RewriteRule rest/(.*)$ /home/rest/$1 [R=301]
</IfModule>
so i want
localhost/rest/abc.php
to
localhost/home/rest/abc.php
Remove the slash before home.
RewriteRule ^rest/(.*)$ home/rest/$1 [R=301]
This is my htaccess:
## Rewrites
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect /stream/ http://twitch.tv/8wayrun
Redirect /stream http://twitch.tv/8wayrun
RewriteCond %{HTTP_HOST} ^(www\.)?8wayrun\.com$
RewriteRule ^(.*)$ http://8wayrun.com/calibur/$1 [R=302,L]
</IfModule>
Basically, I need to to rewrite 8wayrun.com/stream to twitch.tv/8wayrun...
And then I need it to rewrite 8wayrun.com to 8wayrun.com/calibur...
The problem is, its rewriting 8wayrun.com/stream to 8wayrun.com/calibur/stream. How do I fix this?
The Redirect directive is part of mod_alias, and the Rewrite* directives are part of mod_rewrite. When a URI gets processed through the URL/file mapping pipeline, both modules get applied so having one in front of the other doesn't matter, both will get applied in the end.
You're better off sticking with only mod_rewrite and using the L flag to prevent the extra redirects from geting applied.
## Rewrites
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?stream/? http://twitch.tv/8wayrun [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.)?8wayrun\.com$
RewriteRule ^(.*)$ http://8wayrun.com/calibur/$1 [R=302,L]
</IfModule>
I'd like to implement 301 redirection from http://www.onbip.com/index-en.html to http://www.onbip.com/
In htaccess file I have:
RewriteRule ^.htaccess$ - [F,L] #403 Forbidden
RewriteRule ^inc/ - [F,L]
RewriteCond %{HTTP_HOST} ^onbip\.com
RewriteRule ^(.*)$ http: //www.onbip.com/$1 [R=permanent,L]
RewriteRule ^index-([^\.]+)\.html$ index.php?lang=$1 [L]
I Need to standardize the default page which will be http://www.onbip.com/
How?
In your httpd.conf file, there should already be a line to forbid access to .ht* files that will probably look like this:
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>
If you want to be redundent, using Files or FilesMatch to protect it would probably be good. If you want to use Rewrite for this, you could throw a 404 as though it doesn't exist.
Here is a redirect (not a mod_rewrite) for a directory /inc to a 404 page
This is at http://httpd.apache.org/docs/2.0/mod/mod_alias.html
Redirect 404 /inc
Now for rewrite
see http://httpd.apache.org/docs/current/mod/mod_rewrite.html
#Set the page (and order of if they are there) to be shown if asked for a directory
#just put index.php if that's all you want
DirectoryIndex index.php index.html
RewriteEngine on
# if not www.onbip.com, then send to http://www.onbip.com
RewriteCond %{HTTP_HOST} !^www\.onbip\.com [NC]
RewriteRule ^(.*)$ http://www.onbip.com/$1 [R=301,NC,L]
# Now if entered "/index-ab.html" then call "/?lang=ab"
# You might want to see about the regex for proper lang, I put something like "en" or "us-en"
RewriteBase /
RewriteRule ^index-([a-z]{2}(-[a-z]{2})?)\.html$ ?lang=$1 [R=301,NC,L]
The last will call "/" from the server which will be "index.php" first if it exists according to directory index.