I have htaccess file problem. I think my htaccess files conflict.
my .htaccess files:
RewriteEngine on
RewriteRule ^(.*)/video/ video.php?vid=$1 [L]
RewriteRule ^search/(.*)\.html search.php?q=$1 [L]
RewriteRule sitemap-(.*).xml sitemap.php?sayfa=$1 [L]
and my url
Search : http://www.domain.com/search/search_query.html
Video Play : http://www.domain.com/path1/video/video_name.html
.htaccess file conflict video.php and search.php
pls help me
Change order of your rules to keep specific ones first and generic ones later:
RewriteEngine on
RewriteRule ^search/(.*)\.html$ search.php?q=$1 [L]
RewriteRule sitemap-(.*).xml$ sitemap.php?sayfa=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/video/ video.php?vid=$1 [L,NC]
Try using a negative lookahead to prevent the two rules from getting applied to each other:
RewriteEngine on
RewriteRule ^(?!search)(.*)/video/ video.php?vid=$1 [L]
RewriteRule ^search/(?!video)(.*)\.html search.php?q=$1 [L]
Related
I am trying to rewrite a subdirectory out of my URL structure but have other URLs within that directory structure also work. I have this scenario:
// This should load the content of mysite.com/foo/15 but not redirect
mysite.com/foo/
// Subdirectories within /15 should also work
mysite.com/foo/bar // the actual location of this is mysite.com/foo/15/bar
// Other URLs within this directory structure should also work, such as
mysite.com/foo/12
mysite.com/foo/13
mysite.com/foo/14
// Lastly, we have redirects from 2012 -> 12, 2013 -> 13 such that
mysite.com/foo/2012 becomes mysite.com/foo/12
mysite.com/foo/2013 becomes mysite.com/foo/13
... and so on
Here's where i'm at. I can't get the right combination of htaccess rules to achieve what I want.
RewriteEngine On
RewriteBase /foo/
// This makes mysite.com/foo/ load the content of mysite.com/foo/15 without redirecting, but makes mysite.com/foo/14 404
RewriteRule ^((?!15/).*)$ 15/$1 [NC,L]
// This works pretty well, except the rules below don't redirect
RewriteRule ^$ 15/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 15/$1
// Rewrite old years to the two digit form
RewriteRule ^2010/(.*)$ 10/$1 [R=301]
RewriteRule ^2011/(.*)$ 11/$1 [R=301]
RewriteRule ^2012/(.*)$ 12/$1 [R=301]
RewriteRule ^2013/(.*)$ 13/$1 [R=301]
RewriteRule ^2014/(.*)$ 14/$1 [R=301]
RewriteRule ^2015/(.*)$ 15/$1 [R=301]
What am I missing? What combination of rules will help me achieve what i'm looking for?
Your redirect rules can be combined into single rule using regex matching:
RewriteEngine On
RewriteBase /foo/
// Rewrite old years to the two digit form
RewriteRule ^20(1[0-5])/(.*)$ $1/$2 [R=301,L]
RewriteRule ^$ 15/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!15/).*)$ 15/$1 [L]
Try including the L flag in all your rules and then put all your redirects at the top of the htaccess file:
RewriteEngine On
RewriteBase /foo/
// Rewrite old years to the two digit form
RewriteRule ^2010/(.*)$ 10/$1 [R=301,L]
RewriteRule ^2011/(.*)$ 11/$1 [R=301,L]
RewriteRule ^2012/(.*)$ 12/$1 [R=301,L]
RewriteRule ^2013/(.*)$ 13/$1 [R=301,L]
RewriteRule ^2014/(.*)$ 14/$1 [R=301,L]
RewriteRule ^2015/(.*)$ 15/$1 [R=301,L]
// This makes mysite.com/foo/ load the content of mysite.com/foo/15 without redirecting, but makes mysite.com/foo/14 404
RewriteRule ^((?!15/).*)$ 15/$1 [NC,L]
// This works pretty well, except the rules below don't redirect
RewriteRule ^$ 15/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 15/$1 [L]
I want to rewrite the following:
http://www.example.com/map to http://www.example.com/index.php?p=map
and
http://www.example.com/map/1 to http://www.example.com/index.php?p=map&id=1
My current code is:
RewriteEngine On
RewriteRule ^([^/]*)\$ /index.php?p=$1 [NC]
RewriteRule ^([^/]*)/([^/]*)\$ /index.php?p=$1&id=$2 [L]
But if fails...
Who can help me with the right .htaccess code? Thanx in advance!
You need to ignore real files and directories from your rewrite and don't escape $.
You can use:
RewriteEngine On
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /index.php?p=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?p=$1&id=$2 [L,QSA]
I'm using Xfilesharing for our company and I need to block the access to the main upload page by our internal IP range 192.168.230.1-225 and allow the download to every IP.
Using the following rule will block the main page but also the rest. But using a rule to allow the download breaks the rewrite rules.
RewriteEngine on
RewriteRule ^$ /cgi-bin/index.cgi [L]
RewriteCond %{REMOTE_ADDR} ^192\.168\.230\.25[0-5]|2[0-4]\d|1?\d\d?$
RewriteRule ^([0-9A-Z]{12})/([^/]+)/?$ /cgi-bin/index.cgi?act=download1&id=$1&fname=$2 [L,QSA]
RewriteRule ^.*$ /error [R=302,L]
RewriteRule ^del\-([0-9A-Z]{12})\-([0-9A-Z]+)/.+$ /cgi-bin/index.cgi?del=$1-$2 [L]
RewriteRule ^admin$ /?act=admin [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9\-\_]+).html$ /cgi-bin/index.cgi?act=page&tmpl=$1 [L]
The literal URL is http://www.example.com/MG4YBAOW18QH/3.png and the rule: ^([0-9A-Z]{12})/([^\/]+)$ /cgi-bin/index.cgi?act=download1&id=$1&fname=$2 [L]
Have your download rules like this:
RewriteCond %{REMOTE_ADDR} ^192\.168\.230\.\d+
RewriteRule ^([0-9A-Z]{12})/([^/]+)/?$ - [F,NC]
RewriteRule ^([0-9A-Z]{12})/([^/]+)/?$ /cgi-bin/index.cgi?act=download1&id=$1&fname=$2 [L,QSA,NC]
I'm strugglening with my .htaccess file in orther to achieve this:
a.com/male-items (OR)
a.com/male-items/popularity -> a.com/index.php?g=m&sort-popularity
a.com/female-items (OR)
a.com/female-items/popularity -> a.com/index.php?g=f&sort=popularity
a.com/male-items/alphabet -> a.com/index.php?g=m&sort=alphabet
a.com/male-items/alphabet/a -> a.com/index.php?g=m&sort=alphabet&l=a
(and same for female)
I know it should be something like
RewriteRule ^a$ a.com/index.php?q=$1
But actually looking into the different mod-rewrite / regex explanations and cheat-sheets doesn't help a lot with getting it to work. The hard part is to understand how do you define the different parametes in the address and then use them in the rewritten url.
(any explanations with your solution would be appretiated)
Use these rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^male-items/?$ /index.php?g=m&sort=popularity [L,QSA]
RewriteRule ^male-items/([^/]+)/?$ /index.php?g=m&sort=$2 [L,QSA]
RewriteRule ^male-items/([^/]+)/([^/]+)/?$ /index.php?g=m&sort=$2&l=$3 [L,QSA]
RewriteRule ^female-items/?$ /index.php?g=f&sort=popularity [L,QSA]
RewriteRule ^female-items/([^/]+)/?$ /index.php?g=f&sort=$2 [L,QSA]
RewriteRule ^female-items/([^/]+)/([^/]+)/?$ /index.php?g=f&sort=$2&l=$3 [L,QSA]
These htaccess lines redirect all nonexisting files/folders to index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
inside index.php you can use: $_SERVER['REQUEST_URI'] to parse your parameters.
I've been trying all sorts of solutions from this site and none seem to work. I'm currently hosting with hostgator. This is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/user/php.ini
<Files php.ini>
order allow,deny
deny from all
</Files>
</IfModule>
This is in the root folder of my site. I have also tried adding a ? after index.php and no luck. Does anyone know why this isn't working?
This is the code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Symfony 2 has an excellent solution:
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/index.php [L]
This accomplishes the following:
RewriteBase is not necessary (useful when the site is in a subdirectory beneath the web root)
index.php is removed if present
The request is routed to the correct index.php with the full query string from the original request
Note that the line:
RewriteRule ^(.*) - [E=BASE:%1]
is responsible for setting the %{ENV:BASE} variable for later on. Refer to Apache documentation on E|env flag.
I tried this and works fine:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Exceptions
If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:
RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ index.php/$1 [L]
If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
To:
RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
And change:
RewriteRule ^(.*)$ /index.php/$1 [L]
To:
RewriteRule ^(.*)$ testing/index.php/$1 [L]
If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ /index.php?/$1 [L]
To remove index.php from urls on apache2.4 you can use the following rule :
RewriteEngine on
RewriteRule ^index\.php/(.+)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php /index.php%{REQUEST_URI} [L,END]
This will change the uri
/index.php/foobar
to
/foobar
This rule will return an internal server error for lower versions of apache as they don't support the END flag. Please see the anubhava's answer above that works almost on all versions.
I've been compelled to join stack overflow.com today to comment here as the answer has solved a long term problem I've had with a Zend Framework website. I've worked on the website around 3 1/2 years and during that time I discovered that it didn't handle index.php correctly and this causes webmaster tools to see duplicate titles etc.
I decided to search again today for a solution, one of many times attempted.
This is the one (the last answer shown above) that solves my problem.
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
As an example. this is what it achieves for me.
http://www.domainname.co.uk/index.php/categories/url-name.html
becomes
http://www.domainname.co.uk/categories/url-name.html
Thank you to everyone who contributed to the original question as it lead to the answer and solution above.
Extra Note: I have other rewrite commands that handles the other aspects but those on their own didn't fix the index.php and the only time this has been fixed is by using.
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
I only hope it helps others whether its ExpressionEngine or Zend Framework, in the future.