I'm trying to change my .htaccess file so that if I go to:
http://www.example.com/index.php?login=true, it goes to http://www.example.com/login.
I currently have this code which removes index.php (which makes the above looks like http://www.example.com/?login=true).
RewriteEngine On
#remove index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET
I would setup your rewrite rule the other way around:
RewriteEngine On
RewriteRule ^login$ /index.php?login=true
This way if a user browses to http://yourserver.com/login the actual page used is http://yourserver.com/index.php?login=true, but the first URL is shown in the browser. I assume this is what you are trying to achieve.
If you really need to do it in the direction you asked for, you can try something like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^login=true$
RewriteRule ^index\.php$ /login [L,R=301]
This will fail of there are additional query parameters.
If you want to redirect http://yourserver.com/index.php to http://yourserver.com you can simply add the following rewrite rule:
RewriteRule ^index\.php$ / [L,R=301]
The following should work although there might something wrong with the line to exclude /system/*. Try testing with that commented out.
RewriteEngine On
RewriteCond %{QUERY_STRING} ([^=]*)=true [NC]
RewriteCond %{THE_REQUEST} ^/system/.*
RewriteRule index.php /%1? [R=301,L]
The following page helped: http://wiki.apache.org/httpd/RewriteQueryString
This tester is cool but does have a few bugs in it: http://martinmelin.se/rewrite-rule-tester/
try the following in the .htaccess in root directory of example.com
RewriteEngine On
RewriteBase /
#rewrite http://www.example.com/anything to http://www.example.com/index.php?anything=true
RewriteCond %{REQUEST_URI} ^/([-a-zA-Z0-9]+)$ [NC]
RewriteCond %1 !system [NC]
RewriteRule . index.php?%1=true [L]
#301 redirect requests for example.com/index.php?anything=true to example.com/anything
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteCond %{QUERY_STRING} ^([^=]+)=true$ [NC]
RewriteRule . /%1? [L,R=301]
You need to check for the QUERY_STRING in a RewriteCond. I think this should do:
RewriteEngine On
#remove index.php
RewriteCond %{QUERY_STRING} ([^=]*)=true [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule ^index\.php /%1? [R=301,L]
This should redirect anything which has index.php?=true to /action
Related
I have not enough experience in writing rules in .htaccess.
My site url is like
http://exampletest.com/detail.php?i=my-url
I want to rewrite that url to
http://exampletest.com/my-url
What I have tried so far:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} /detail\.php\?i=([^\s&][A-Za-z0-9-]+) [NC]
RewriteRule ^ detail %1? [R=302,L]
That makes me able to access my site using:
http://exampletest.com/detail/my-url
But I want to omit detail from url as well.
You should redirect internally as well so your code should looks like this :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} /detail\.php\?i=([^\s&][A-Za-z0-9-]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /detail.php?i=$1 [L]
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} /detail\.php\?i=([^\s&][A-Za-z0-9-]+) [NC]
RewriteRule ^ %1? [R=302,L]
I would like redirect http://example.com/index.php?a=music to http://example.com/music (where the number is a variable/dynamic). My site is http : / / example . com
I added the following line to my mod-rewrite rules in my .htaccess file:
RewriteRule ^index\.php$/?/a/=(.*)$ http://example.com/index.php?a=$1 [L,R=301]
However, it doesn't seem to work. I know I'm doing something wrong, I'm just not sure how to fix it. Any help is appreciated.
Best,
Jeff
Edited:
Thanks Tom,
This is what I have:
Rewritecond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
<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]
This should work:
RewriteRule ^(.*)$ /index.php?a=$1 [L]
Please read the question carefully before marking as duplicate.
We all know, that using in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
we can redirect all traffic to index.php so we can create friendly urls and have one front controller.
Although the question is connected to mod_rewrite the problem is described for Laravel.
The following .htaccess comes by default with Laravel 4 and it works fine:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If we run url mydomain.com/something and have set that route for something properly, some controller will be launched. It works fine so far.
However in Laravel 4 we will be able to reach the same route using mydomain.com/index.php/something. Probably using Laravel url creating we will have no urls with index.php in url but there is some other problem.
For example if our competition would like to make us some harm, they can simple put in Internet single links for urls to mydomain.com/index.php/something, mydomain.com/index.php/something2 and so on and search engines will see duplicate urls.
Of course if we have our custom PHP application, we can do it in PHP without a problem checking simply $_SERVER['REQUEST_URI'] and make 301 redirection. We can of course do the same in Laravel but we have to write this code in PHP each time and probably some developers could say it is bad practice to do it in PHP.
Question is simple: how can I redirect in .htaccess all urls that contain index.php to to the same url without index.php?
Example urls that should be redirected:
mydomain.com/index.php/something should be redirected to mydomain.com/something (something could be anything - can contain any characters)
mydomain.com/index.php should be redirected to mydomain.com
mydomain.com/index.php?anything should be redirected to mydomain.com (anything can contain any characters)
mydomain.com/index.phpanything should be redirected to mydomain.com anything can contain any characters)
Insert these rules just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
After spending hours I write below code for me and its 100% working
Redirect index.php to non index.php
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^index.php/(.*)$ /$1 [R=301,L]
how can I redirect in .htaccess all urls that contain index.php to to
the same url without index.php?
Add this to your .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
For Nginx, here is the rules :
location / {
rewrite ^/(.*?)index\.php[^/] /$1? redirect;
rewrite ^/(.*?)index\.php(?:/(.*))?$ /$1$2? redirect;
}
This solved my problem to force https & remove index.php from the url in Kohan 2.3
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
RewriteRule ^(application|system) - [F,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Simplified Question
I changed domain.com/folder/client.php?id=1 to 1.domain.com with this .htaccess code
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
Now I want to change domain.com/folder/about.php?id=1 to 1.domain.com/about.
Is it possible ? or is there any other ways to do this?
Thank you :D
Old Question
In my directory, I have 2 pages (client.php, about.php). I've already set a wildcard subdomain to the page directory and changed domain.com/folder/client.php?id=1 to 1.domain.com. This is my .htaccess code :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
Now, I need to add another page. So, in client.php page, there's a button to go to about.php page in the same directory. I tried 1.domain.com/about.php and it's not working.
Do I have to create another line of RewriteCond ? or is there any other ways to do this?
Thank you very much :D
Updated Code without working id
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/about [NC]
RewriteCond %{REQUEST_URI} !about\.php [NC]
RewriteRule . /about.php?id=%1 [L,QSA]
**The code resolved the text in url box. Let's say id=1. Now it's showing 1.domain.com/about.
the php code : `$_GET["id"]` is not specified.**
Thanks to faa for the edit :D
The Full Solution
Refer Using mod-Rewrite with 5 pages (wildcard sub-domain)
You may try this:
Options +FollowSymLinks
RewriteEngine on
## This is the actual rule-set that works as it is according to the OP
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
# Added to exclude folder `/about`
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteRule ^$ /client.php?id=%2 [L,QSA,NC]
## This is the additional rule set
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/about [NC]
RewriteCond %{REQUEST_URI} !about\.php [NC]
RewriteRule . http://domain.com/folder/about.php?id=%1 [L,QSA]
Maps silently
http://N.domain.com/about with or without trailing slash
To:
http://domain.com/folder/about.php?id=N
For permanent and visible redirection, replace [L,QSA] with [L,R=301,QSA]
I have a search form that sends a GET request to a page called search.php. I have rewrite rules set up in my htaccess file that rewrite certain things, like /search, to their respective pages. I simply want to take the search.php?q=query and rewrite it to /search/query.
Here is what I have.
RewriteRule search.php?q=(.*) /search/$1
RewriteRule search/(.*) search.php?q=$1 [nc]
What am I doing wrong?!
Here is the complete file
ErrorDocument 404 /index.php?p=404
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?wghandcrafted.com/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|psd|js|swf|flv|png)$ /feed/ [R=302]
RewriteRule ^(products|blog|feed|search|checkout|checkout)$ $1.php [nc]
RewriteRule products/cat/(.*)$ products.php?type=cat&cat=$1 [nc]
RewriteRule products/(.*)$ products.php?type=single&product=$1 [nc]
RewriteRule blog/(.*) blog.php&post=$1 [nc]
RewriteRule feed/(.*) feed.phptype=$1 [nc]
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
Make the first line perform a Redirect and the second perform a Rewrite
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
and move
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
to the end of the set of rules.
Otherwise, the RewriteCond %{REQUEST_FILENAME} !-f rule is enacted before anything else, meaning that only requests for non-existant files will be handled by any rules below that line. As there is a search.php file, this prevents that rule from ever being reached.
I was having the same problem and here is a solution i found that worked for me, on my site the queries are being sent to index.php, I discovered if I had "^index.php$ /search/%1? [R=301]" as the first rewrite rule it will just error out because of the second rewrite rule making it just go in a loop so i replaced "^index.php$" with "^$" allowing it to still request the same file. It might not be the best solution, but a work around that works. Here is my working code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^$ /search/%1? [R=301]
RewriteBase /search
RewriteRule ^search/(.*)$ /index.php?q=$1 [NC]