I'm trying to create shorter urls to some of my pages.
Previously I had a system that URLs were like /index.php?m=page&title=Page-Title
The piece of code I have now allows me to remove the everything 'm=' part. /index.php?m=page in here, the 'page' is a name of different modules, so this part might still change. I want to change the url only from 'page'.
How would I rewrite this so that my URL would be composed only as such:
/page=Page-Title
Would that be even possible since I'm using $_GET in 2 places?
My current .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L]
You can have your rules like this:
RewriteEngine On
RewriteRule ^page=([^/]+)/?$ /index.php?p=page&t=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L,QSA]
Related
I wish to rewrite this url:
http://salessurface.com/overseas/sub_page_details/MBBSAbroad/MBBS-in-UK/7
into:
http://salessurface.com/overseas/sub_page_details.php?sup_menu=MBBS%20Abroad&menu=MBBS-in-UK&main_menu_id=7
I tried adding the following to the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ overseas/sub_page_details.php?sup_menu=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ overseas/index.php?sup_menu=$1&menu=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ overseas/index.php?
sup_menu=$1&menu=$2&main_menu_id=$3 [L]
but it is not working.
How do I accomplish that rewrite?
You don't need the conditions, you can do that with just a rule:
RewriteEngine on
RewriteRule ^overseas/sub_page_details/(.*)/(.*)/(.*) /overseas/sub_page_details.php?sup_menu=$1&menu=$2&main_menu_id=$3 [NC,L]
This will take the path after sub_page_details and put the first part in sup_menu, the second part in menu and third part in main_menu_id.
But I noted that in your sample URLs you use MBBS%20Abroad in the real URL but MBBSAbroad in the rewritten URL, if this is not a typo and the real URL has an space on it that doesn't have the rewritten URL then you need to use a less specific rewrite for that option:
RewriteEngine on
RewriteRule ^overseas/sub_page_details/MBBSAbroad/(.*)/(.*) /overseas/sub_page_details.php?sup_menu=MBBS\ Abroad&menu=$1&main_menu_id=$2 [NC,L]
This will hard code the sup_menu part of the URL and take the path after MBBSAbroad and put the first part in menu and second part in main_menu_id.
This question already has an answer here:
.htaccess exclude a directory to be rewrited
(1 answer)
Closed 9 years ago.
I have a general rewrite-rule in my sites root which looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
which works fine for rewriting URLs like "www.example.com/myAction" to "www.example.com/index.php?action=myAction"
This must stay like this, but I need to exclude a subdirectory ("/login") from rewriting.
So I tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/login/
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
but still no luck. I've read several answers here on SO but can't find the flaw. Any ideas? Thank you!
You've an extra slash. .htaccess doesn't use the preceding slash unlike a Virtual Host. Also /login/ is a directory. So you're matching anything that is not a real file, but could potentially be a real directory. So if for instance '/css/' exists, you're rewriting it to index.php as an action on request.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+) index.php?action=$1
If it's still acting unexpectedly you might need to make sure you're not rewriting existing directories and try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+) index.php?action=$1
I also noticed that you're only capturing up to the first slash (possibly) with your posted example code since you're using the ? look-up, so in my previous layout's I'm showing that code a little shorter. To capture the whole URI you would use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^(.*) index.php?action=$1
To capture a second (and third) string you can do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+)/([^/]+)/([^/]+) index.php?action=$1&page=$2&response=$3
What you have should work, but you can alternatively try to have all requests for /login/ be passed through instead:
RewriteEngine On
RewriteRule ^login/? - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
I currently have this set up and working fine inside a users folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?i=$1
e.g 127.0.0.1/site/users/admin goes to 127.0.0.1/site/users/index.php?i=admin
Now as this is for a profile page, how would i do something such as this.
users/admin/activity
So that it would show the activity page for that user? I am totally confused on how i would go about this.
Would it be best to make the index.php accept a page $_GET variable? But the how would i get htaccess to work around it?
You rules will look something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ index.php?i=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?i=$1 [L]
Now your index.php should be getting 2 $_GET variables, i and page.
I've got most of the rewrites I need working but I can't get the second part working with 2 variables, here is my code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ entity.php?vanityName=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /entity.php?vanityName=$1§ion=$2 [L]
The last bit needs to allow, for example, http://example.com/vanityName/Section however it doesn't pass the section variable.
How can I fix this while retaining the current rewrites?
How can I get it so it works for /vanityName, /vanityName/section but allows me to keep other directories free of rewriting, like /includes/?
You can add an extra rule for every directory you don't want to rewrite:
RewriteRule directoryName/? - [L]
If you write these rules directly after RewriteBase /, no other rules will be checked.
I've been using this htaccess code to pass on vars to redirect.php which handles the includes for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)/(.*)$ redirect.php?value1=$1&value2=$2&value3=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)$ redirect.php?value1=$1&value2=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ redirect.php?value1=$1 [L,QSA]
but i noticed that it will not go to redirect if all arguments is empty, so if i go to http://domain.com/ it will open index.php, but if i go to http://domain.com/any-param/ redirect.php handles it correctlty. How can I make it always use redirect.php as default, even when no additional URL parameters is set?
Your rules appear to be fine to me. Just add this line in the end:
RewriteRule ^$ redirect.php [L]
This will redirect http://domain.com/ to http://domain.com/redirect.php.
use to do it like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z]+)?/?([a-z]+)?/?([-a-z0-9]+)?/?$ [NC]
RewriteRule ^([a-z]+)/?([a-z]+)?/?([-a-z0-9]+)?/?$ /redirect.php?value1=$1&value2=$2&value3=$3 [NC,L]
Note: Even with only 1 param, this will still work as it just leaves the other 2 blank. So you don't have to copy and paste thew same code while removing just one condition.
I found that this is not very scalable. Now I just redirect everything to the index.php and let that file fetch and dissect the URI to handle my system layout. This allows for a very scalable system.