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.
Related
Hi there I'm trying to make a blog demo and I'm having some pretty URL codes already.
i am having a url www.xyz.com
and a search url www.xyz.com/search/this+is+a+search+text
in search url the parameter search is a page name and this+is+a+search+text is a parameter that i'll be parsing
I'm having a .htaccess code below already
# code to make pretty URLS | we're using this code to achieve /category/slug
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\d]+)$ app/index.php?page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)$ app/post.php?category=$2 [L]
I'm using the below code for serach page
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php&searchstring=$2&page=$3 [L,QSA]
But while using the code I get a 500 internal error message, I'm not able to figure out what's the error!
I would really appreciate if anyone could help me out with this logic.
Rewrites in achieve /category/slug and for search page are the same. Search request matched with first - achieve rewrite and run app/post.php...
To answer your followup question to JarekBaran
JarekBaran: Rewrites in achieve /category/slug and for search page are the same.
What this means is that the RewriteCond are the same for the pretty URL code for the caterogy/slug page and the search page.
This means that the first match will always be used and so:
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
will always be triggered before
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
This way you will not be able to use the search function. You could consider adding a parameter to identify if a search is made or not.
Your problem is that you are starting your rules with (.+) which will match one or more of anything. This means that you are also matching the / character. Some of your 2 parameter redirects are matching your urls with 3 parameters because of this. You would be better of starting with something like this - ([a-zA-Z0-9-]+)
Your search rewrite should probably be something like this - RewriteRule ^/?([a-zA-Z0-9-]+)/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
I tested that rewrite on this htaccess testing tool and it is working.
That said your other rules will need to be changed as well since they all start with (.+). After doing that your category rewrite will conflict with your search rewrite.
However, this would probably work for you:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?search/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9-]+)/([\d]+)$ app/index.php?page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)$ app/post.php?category=$2 [L]
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.
I have a wordpress site and a custom build app all on the same URL.
I am looking to redirect all my CMS (custome app) URLs to a certain php controller file. Once redirected I am looking to remove the php file from the URL
e.g. www.website.com/cms redirects to www.website.com/backend.php/cms but the URL continues to display as www.website.com/cms
So far I have the following:
# Handle the custom App URLs
RewriteCond %{HTTP_HOST} ^(www.)?website.com/cms$
RewriteCond %{REQUEST_URI} !^backend.php/cms/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ backend.php/cms/$1
# Handle the Wordpress URLs by removing index.php
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
EDIT:
The problem is what I have does not work.
If I go to www.website.com/cms I get a 404.
If I go to www.website.com/backend.php/cms the URL does not change to www.website.com/cms, it stays as www.website.com/backend.php/cms
I am not great at .htaccess but think I am going down the correct route.
Did you try just routing by url:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/cms/(.*)$ /backend.php [L]
#or RewriteRule ^/cms/(.*)$ /yourcms/backend.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
All your magic about 'understanding' the url you do in your cms reading REQUEST_URI.
I ended up fixing this myself by having the following RewriteRule
RewriteRule ^/?cms([A-Za-z0-9\-_\/]+)/?$ /backend.php [L]
RewriteRule ^/?cms /backend.php [L]
Thanks to those that tried to help :)
Well,
This is my actual url:
http://www.domain.com/wix/t1/index.php?username=foyezbd (this foyezbd is come from variable $username)
and I re-write it with .htaccess
http://www.domain.com/wix/t1/foyezbd
.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wix/t1/index.php?username=$1 [L]
Now I've another page which is page.php
so the actual link for this page might be..
http://www.domain.com/wix/t1/page.php?username=$username&menu_name=$menu_name
so I want to re-write this url with following structure: (For example $username = foyezbd and $menu_name = About us)
http://www.domain.com/wix/t1/foyezbd/aboutus
How can i do this with .htaccess ? I added a new line like:
RewriteRule ^(.*)$ /wix/t1/page.php?username=$1 [L]
but it says Internal Server Error!
You're getting a server error because the rule is looping. You don't have conditions to prevent this like you do for the first rule. Additionally, (.*) is too general if you want to be able to differentiate between the two:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /wix/t1/page.php?username=$1&menu_name=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /wix/t1/index.php?username=$1 [L]
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.