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?
Related
I have made a custom PHP/Sql cms with a dynamic post template called blog-post.php
I am pulling in the content like so:
http://www.example.com/news/blog-post.php?slug=sample-slug-from-database
This allows me to pull in any number of posts using the same template, pulling info from the database.
I have spent 4 hours trying to achieve the following with htaccess ..
Force a trailing slash instead of .php extension
Force blog post urls to show as:
http://www.example.com/sample-slug-from-database/
instead of
http://www.example.com/news/blog-post.php?slug=sample-slug-from-database
Any help is greatly appreciated.
Although your preferred method of URL is to redirect everything, e.g. /sample-slug-from-database/, I think it would be better to instead go with a less strict implementation, e.g. /news/sample-slug-from-database/
Either way, here's the first implementation:
# URL
# INPUT http://www.example.com/random-slug/
# OUTPUT http://www.example.com/news/blog-post.php?slug=random-slug
RewriteEngine On
RewriteRule ^(.*)\/$ news/blog-post.php?slug=$1 [L,QSA]
And the second way:
# URL
# INPUT http://www.example.com/news/random-slug/
# OUTPUT http://www.example.com/news/blog-post.php?slug=random-slug
RewriteEngine On
RewriteRule ^news\/(.*?)(\/(.*?)*)$ /news/blog-post.php?slug=$1 [L]
For anyone else struggling - this is my final code:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteEngine On
RewriteRule ^(.*)\/$ blog-post.php?slug=$1 [L,QSA]
My url is like this: http://example.com/blog/blog.php?id=38
and in blog.php I am using $_GET and showing the title and all descriptions about the blog.
However, I want to make it something like this: http://example.com/blog/this-can-be-blog-title
I've tried this .htaccess to make it at least look like this : http://example.com/blog/blog/id/38/
Options +FollowSymLinks
RewriteEngine on
RewriteRule blog/id/(.*)/ blog.php?id=$1
RewriteRule blog/id/(.*) blog.php?id=$1
But even this doesn't work.There is no change in the url. What am I missing ?
Try this .htaccess
RewriteEngine on
RewriteBase /
# mod_rewrite ignore rules if a matching file or directory exists
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
# generate url
RewriteRule ^(.*)$ blog/blog.php?url=$1 [NC,L,QSA]
You will need to get the proper page from your database that has "this-can-be-blog-title" slug via URL GET parameter and no more query based on id.
If you just want to pass id as GET parameter use this:
RewriteEngine on
RewriteBase /
# mod_rewrite ignore rules if a matching file or directory exists
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
# generate url
RewriteRule ^blog/id/(.*)$ blog/blog.php?url=$1 [NC,L,QSA]
You can use:
Options +FollowSymLinks -Multiviews
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+blog/blog\.php\?id=(\d+) [NC]
RewriteRule ^ /blog/id/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^blog/id/(\d+)/?$ blog/blog.php?id=$1 [L,QSA,NC]
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?
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]
I am looking for some guidance when working with a maintenance mode system, utilised within a .htaccess file, via mod_rewrite and passing a $_GET along with it.
Passing a $_GET in mod_rewrite is perfectly fine under normal circumstances for me, but in this instance I am experiencing issues..
The code I have for the maintenance system is as follows:
RewriteCond %{REMOTE_HOST} !^XX\.XXX\.XXX\.XXX
RewriteCond %{REQUEST_URI} !/maintenance.php$
RewriteCond %{REQUEST_URI} !^/css/.*$
RewriteCond %{REQUEST_URI} !^/assets/.*$
RewriteRule ^$ maintenance.php [R=302,L]
So what I need is the ability to pass a $_GET along with the rewriteRule, thus when the site is viewed as normal by anyone using the allowed IP, I can define that the site is being viewed in maintenance mode.
Of course people not on the allowed IP get redirected to maintenance.php file and don't need this reminder anyway, as the page does that already.
Thank you in advance anyone that can help me in this issue.
EDIT::
# Start the mod re-write conditions #
RewriteRule ^([^/]+)\/$ ?cat=generic&page=$1 [L]
RewriteRule ^products\/([^/]+)\/([^/]+)\/$ ?cat=product&page=$2 [L]
RewriteRule ^theteam\/([^/]+)\/$ ?cat=staff&page=$1 [L]
Thats how I deal with the other links on my page, I hope that is all you needed to see.
Dan.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !maintenance.php
RewriteCond %{REQUEST_URI} !(css|assets).*$
RewriteCond %{REMOTE_ADDR} !^XX\.XXX\.XXX\.XXX$
RewriteRule (.*) /maintenance.php [R=302,L]
RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteRule (.*) $1?mode=maintenance [L,QSA]
This works for me, first you have the negation redirect so whoever tries to access without the allowed IP will go to maintenance.php.
Then you have the append internal redirect, the GET mode=maintenance will not be visible but will be there.
And you can retrieve it with $_GET['mode'].
If you want it to visually append to the query string and only to php files you can use:
RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteCond %{QUERY_STRING} !^mode=maintenance.*$
RewriteRule (.*) $1?mode=maintenance [R,L,QSA]
Update of visible rule for your sub directories format this rule should be placed before it like this:
RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteCond %{QUERY_STRING} !.*mode=maintenance.*$
# for visible query string uncomment the below line and comment the next rule
#RewriteRule (.*) $1?mode=maintenance [R,L,QSA]
# for invisible query string
RewriteRule (.*) $1?mode=maintenance [L,QSA]
# Start the mod re-write conditions #
RewriteRule ^([^/]+)\/$ ?cat=generic&page=$1 [L,QSA]
RewriteRule ^products\/([^/]+)\/([^/]+)\/$ ?cat=product&page=$2 [L,QSA]
RewriteRule ^theteam\/([^/]+)\/$ ?cat=staff&page=$1 [L,QSA]