I am trying to create an htaccess script that will create directories for either zero, one, or two variables.
My file that is being used accepts the following 2 get variables. make and model.
This is a 3 step page. I currently have the page located at at /new.php. This page allows a user to select the first variable (in my case, a vehicle make). By selecting a make on this page the user is taken to /new.php?make=Acura. This page now displays a list of all Acura models. From here, the user can click a model, and they will be directed to /new.php?make=Acura&model=TLX. They can now choose a submodel and will be taken to an information page.
So I'm trying to get:
new.php to go to /new/
new.php?make=XMake to go to /new/XMake/
and new.php?make=XMake&model=XModel to go to /new/XMake/XModel/
This is as far as I have gotten with my code:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^new/(.*)$ new.php?make=$1 [L,NC]
However any variables I add after this seem to break the first directory? Why is this?
You can use these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^new/([\w-]+)/([\w-]+)/?$ new.php?make=$1&model=$2 [L,NC,QSA]
RewriteRule ^new/([\w-]+)/?$ new.php?make=$1 [L,NC,QSA]
RewriteRule ^new/?$ new.php [L,NC]
The order of the rules is important. With this rule at the beginning, any request /new/, /new/XMake or /new/XMake/XModel/ matches, and the following rules are ignored.
To match the other rules, the more specific must come first, e.g.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/(.+?)/(.+)$ new.php?make=$1&model=$2 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/(.+)$ new.php?make=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/$ new.php [L,NC]
Related
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]
I currently have a shortner for a profile page. I now want to add an additional shortner to a tag page.
CURRENT CODE (for profile - works fine):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /cd/profile.php?username=$1
My problem is when I want to differiantiate the pattern to add the tag page:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/()$ dh/profile.php?username=$1 [NC,L]
RewriteRule ^tag/()$ cd/tags.php?tag_name=$1 [NC,L]
Now, neither works. I am testing it using XAMP and my domains at the moment would be:
1) Directory for profile --> localhost/cd/profile.php?username=John
2) Directory for tags --> localhost/cd/tags.php?tag_name=blue
Do I have to restructure the root to each page? How can I make it work?
Cheers
Your regex are incorrect in both rules.
Try this code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^user/(.+)$ /dh/profile.php?username=$1 [NC,L,QSA]
RewriteRule ^tag/(.+)$ /cd/tags.php?tag_name=$1 [NC,L,QSA]
Also remember that RewriteCond is only applicable to the very next RewriteRule.
I've been assigned to a new website and I need to incorporate page routing and "vanity URLs" with .htaccess
I'm really not familiar with htaccess, and despite all my research efforts, I have not gotten any solid answers.
The website has a custom-built PHP admin system located in a subdirectory called "admin". I don't want to mess with it at all, so whatever changes I made to htaccess cannot affect that subdirectory.
So, given that the page has 3 main "pages": home, buy, and sell, I need requests made to "www.sitename.com" and "www.sitename.com/index.php" to route to the homepage. I need requests made to "www.sitename.com/sell" to route to the sell page.
And then the hardest part, I need requests made to "www.sitename.com/buy" to route to "buy.php", but requests made to "www.sitename.com/buy/category-name" to route to "www.sitename.com/buy.php?c=12"
I know its a lot to ask, but if anyone can guide me in the right direction, it would be much appreciated.
These rules should work for you:
RewriteEngine On
RewriteBase /
# skip admin OR any valid file/dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} /admin/ [NC]
RewriteRule ^ - [L]
RewriteRule ^(sell|buy)/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(buy)/([^/]+)/?$ $1.php?c=$2 [L,NC,QSA]
Try this:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^sell$ ./sell.php
RewriteRule ^buy$ ./buy.php
RewriteRule ^buy/(.*)$ ./buy.php?c=$1
If the category is always numeric replace the (.*) with (\d+)
I think this hope
/* copy and paste below, save the name. htaccess */
RewriteEngine On
RewriteBase /yousite.com/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
#ex1:http://yousite.com/buy/3
RewriteRule ([a-zA-Z_-]+)/([0-9]?) buy.php?c=$1
/* and in your file php */
<!-- #ex1:http://yousite.com/buy/3 -->
buy/3
I am trying to change the below rewrite rule to accept more folders. Currently any other url else mydomain.com\test will be directed to index.php.
RewriteRule ^((?!test).)*$ index.php [QSA,L]
I need to add more subfolders like test1, test2 etc. So that I can access url \test1, \test2 etc.
Rewrite Rule
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!test).)*$ index.php [QSA,L]
Well...
What you have already also matches /test1 and /test2 because the regex you have only checks that something doesn't start with /test, and both /test1 and /test2 starts with /test.
But if you want it to check multiple folders that don't all start with the same thing, then use the | in the expression. Say you also want to exclude /blah and /bleh:
RewriteRule ^((?!test|blah|bleh).)*$ index.php [QSA,L]
one of many ways:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_URI} !^/test1/
RewriteCond %{REQUEST_URI} !^/test2/
RewriteRule ^(.*)$ index.php [QSA,L]
I am trying to redirect pages using mod_rewrite to the pages with some variables (for using them with PHP's $_GET).
To give an example with few lines:
When the user enters /c/stg it redirects to item_show.php?id=$1&f=1 (where f is page number).
When the user enters /c/stg/2 it redirects to the second page with show.php?id=$1&f=$2.
I think there are no errors or misuses of these in my file but, here's what I want:
I want user to enter /string directly to go item_show.php?id=$1&f=1 with $1 is our string of course...
But when I change my file by removing the /c/ part from RewriteRule it starts giving errors in all other directories and doesn't read any files (my.css) even though I have already defined a RewriteCond %{REQUEST_FILENAME} !-f...
Do you have any suggestions?
Or how can I made this system possible with any method?
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#user
RewriteRule ^u/([^/]+)/?$ profile.php?username=$1 [L,NC]
#marked
RewriteRule ^marked/([^/]+)/?$ item_marked.php?id=$1 [L,NC]
#content
RewriteRule ^c/([^/]+)/?$ item_show.php?id=$1&f=1 [L,NC]
RewriteRule ^c/([^/]+)/([^/]+)/?$ item_show.php?id=$1&f=$2 [L,NC]
Am I wrong if RewriteCond only are passed on to the first RewriteRule??
Try this instead:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#user
RewriteRule ^u/([^/]+)/?$ profile.php?username=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#marked
RewriteRule ^marked/([^/]+)/?$ item_marked.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#content
RewriteRule ^c/([^/]+)/?$ item_show.php?id=$1&f=1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ item_show.php?id=$1&f=$2 [L,NC]
Cheers,
RewriteCond directives do only belong to the first following RewriteRule directive. So in your case the two RewriteCond direcitves are only applied to the first RewriteRule directive and not to the others.
But you could use this abortion rule to quit the rewrite process if the request can be mapped to an existing file or directory:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
This would cause that following rules are only tested if the request cannot be mapped directly to an existing file or directory.