I am knocking my head how to rewrite get parameters with htaccess.
Here is my htaccess so far (removing only index.php).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I want to rewrite the following url http://example.com/?lang=en&dscroll=1200 into http://example.com/en/dscroll/1200. Some htaccess master, please :)?
Thanks
The rule that you have doesn't remove the index.php, it only redirects to a host without the www in front.
As for the other rewrite, you need 2. First, you need to make sure your links in all of your content looks like this:
http://example.com/en/dscroll/1200
instead of the one that has the query string.
Then you need to add, below the rule that you already have, these 2 rules:
RewriteCond %{THE_REQUEST} \ /+\?lang=([^&]+)&([^=&\ ]+)=([^&\ ]+)
RewriteRule ^ /%1/%2/%3? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /?lang=$1&$2=$3 [L,QSA]
Related
My request is quite simple. Using my current .htaccess conditions and rules as given here:
# Remove .php extension from URLS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# Redirect from *.php to URL without *.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
Problem is, when I pass a URL that contains *.php?param1=A¶m2=B as a parameter, it throws away "?param1=A¶m2=B"
For example:
I want to redirect to: "/views/users/login.php?redirect=/views/home.php?id=1"
Resulting in: "/views/users/login?redirect=/views/home", which throws away "?id=1", so now I can not access that parameter.
How do I rewrite my rules so that it keeps those parameters?
Any suggestions are welcome and much appreciated.
Update (2015-09-16):
Removing
RewriteRule ^(.*)\/index\.php$ $1 [R=301,L,NC]
As it is irrelevant.
You'll have to use urlencode to encode the URL into a parameter.
So when building the link or redirect, use:
redirect('views/user/login.php?redirect=' . urlencode('/views/home.php?id=1'))
btw: redirecting to a "controller" in a folder called "views" might be a bit confusing in a few month :)
Hello you just need to add the query append marker like so:
RewriteRule ^(.*)\/index\.php$ $1 [QSA,R=301,L,NC]
that is "QSA"
I have solved this issue (to some agree) using the following code:
# Enable rewrite mod.
RewriteEngine On
RewriteBase /
Options +FollowSymLinks -MultiViews
# Redirect URLs that contain *.php to extensionless php URLs.
RewriteCond %{THE_REQUEST} ^(.*)\.php
RewriteRule ^(.+)\.php$ $1 [R,L]
# Resolve *.php file for extensionless php URLs.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php [NC,L,QSA]
# Resolve /views/* for URLs that do not contain views and needs it.
RewriteCond %{REQUEST_URI} ^/applications/(.*)$ [OR]
...
RewriteCond %{REQUEST_URI} ^/home [OR]
...
RewriteRule ^(.*)$ /views/$1
# Redirect URLs that contains /views/* to viewsless URLs.
RewriteCond %{THE_REQUEST} ^(.*)/views/(.*)
RewriteRule ^views(.*)$ $1 [R,L]
I have crawled StackExchange for 3 days now and come close to finding the solution to my problem, but keep coming up short.
I am using htaccess to rewrite and redirect urls to SEO friendly urls. My htaccess currently is as follows.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
########################################
# REMOVE INDEX.PHP FROM THE URL
########################################
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
##################################################
# REWRITE QUERY STRING INTO SEO FRIENDLY URL
##################################################
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?page=([^\s]+)? [NC]
RewriteRule ^ /%1? [R=301,L]
Currently my links pass 1 main argument '?page=somepage'. On my users page, I have links that pass not only the page argument, but also the id of a user. That link is as follows :
?page=users&id=1
Furthermore, a user has the option to edit their own profile, so that link is like this :
?page=users&id=1&do=edit
My htaccess handles the rewrite and redirect of the url correctly, but only when the {QUERY_STRING} has just one argument that is passed.
I have played with my existing RewriteCond to look for multiple arguments in the query string and also tried changing the RewriteRule to handle multiple arguments in the query string. I was successful when dealing with 2 arguments, but my original rule (for 1 argument) broke.
How should I go about writing my RewriteCond / RewriteRule to handle urls with either 1 or or more {QUERY_STRING} arguments?
I want my urls to go from :
?page=users
?page=users&id=1
?page=users&id=1&do=edit
to this :
/users
/users/1
/users/1/edit
After tinkering with my htaccess file, I managed to find a solution to my problem. This just proves that often the answer you are searching for is the most obvious and most overlooked. The following is my updated, and working htaccess file.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
########################################
# REMOVE INDEX.PHP FROM THE URL
########################################
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
##################################################
# RERWITE QUERY STRING INTO SEO FRIENDLY URL
##################################################
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?page=([^\s]+)? [NC]
RewriteRule ^ /%1? [R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?page=([^\s]+)&id=([^\s]+)? [NC]
RewriteRule ^ /%1/%2? [R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?page=([^\s]+)&id=([^\s]+)&do=([^\s]+)? [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
While this is working for my needs, I do have another question.... Is there a way to simplify my htaccess RewriteCond / RewriteRule in this file?
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]
I am new to htaccess mod_rewrite rules. I want to eliminate the GET variables from the URL. I have tried the following rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule (.*) /$1? [R=301,L] #remove query string
The above rule eliminated the query ?id, but it redirected the URL as www.example.com/admin/test.php without the subdirectory project and with the extension .php
I want to convert
www.example.com/project/admin/test.php?id=1
to
www.example.com/project/admin/test
Browsing www.example.com/project/admin/test?id=1 would be the same as www.example.com/project/admin/test.php?id=1
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php
RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^(v)$ /$1/
# The following rule does the rewrite.
RewriteRule ^(.+)/$ profile.php?name=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^profile.php$ %1?
I'm doing this code above in my .htaccess file, this works fine in my profile.php page because before, my profile.php is displaying this kind of URL:
http://mysite.com/profile.php?name=john
but when I wrote that code this is the result:
http://mysite.com/john/
I also wanted to apply this code to my category.php, so what I did was just to copy the code above and my whole .htaccess file looks like this one:
RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^(v)$ /$1/
# The following rule does the rewrite.
RewriteRule ^(.+)/$ profile.php?name=$1
RewriteRule ^(.+)/$ category.php?cid=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^profile.php$ %1?
RewriteCond %{REQUEST_URI} ^/category.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /category.php
RewriteCond %{QUERY_STRING} cid=([^&]+)
RewriteRule ^category.php$ %1?
But this code gives me a weird result because when I visit
http://mysite.com/john/ the content that i'm seeing is the content of my category.php.
What should I do here? Any help would be greatly appreciated and rewarded!
Thanks!
i have got the problem in ur rule. the problem what i suppose is that your webserver is unable to differentiate the request is of category page / profile page whenever an request comes like
http://mysite.com/john/
so i suggest do one change in either profile page request or in category page request.
simply add http://mysite.com/profile/john/
hope this works out.
To answer the question as it was modified in the comments:
To differentiate between users and categories if these terms appear in the url, you only need one line per rule (untested example):
RewriteEngine On
RewriteBase /
RewriteRule ^user/(\w+)/?$ /profile.php?name=$1 // using only a limited set of chars (letters and underscore) for the user name and an optional / at the end
RewriteRule ^category/(\w+)/?$ /category.php?cid=$1 // using only a limited set of chars for the category (letters and underscore) and an optional / at the end
Looks like I solved it using this one:
RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^user/(!(profile.php))$ user/$1/ [R=301,L]
RewriteRule ^category/(!(category.php))$ category/$1/ [R=301,L]
# The following rule does the rewrite.
RewriteRule ^user/(.+)/$ profile.php?id=$1
RewriteRule ^category/(.+)/$ category.php?id=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^profile.php$ user/%1?
RewriteCond %{REQUEST_URI} ^/category.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /category.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^category.php$ category/%1?