I have two re-write rules one as a vanity url so for example domain.com/url and one to edit some a data aka post.
For some reason only the first rule is taking place and not both
Why is that and how can I fix it??
Here's The Code
RewriteEngine On
# Make sure you only match on files/directories that don't actually exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
# Rewrite the first part after the `/` into a `username` parameter
RewriteRule ^(.+)$ groups/index.php?gname=$1 [L,QSA]
RewriteRule ^edit/id/([0-9]+)/?$ groups/view_update.php?pid=$1 [NC,QSA,L]
RewriteCond is only applicable to very next RewriteRule not to all the RewriteRule. Also change order of your rules.
Change your code to this:
RewriteEngine On
# Make sure you only match on files/directories that don't actually exist
RewriteBase /
# Rewrite the first part after the `/` into a `username` parameter
# skip all rewrite rules for file or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^edit/id/([0-9]+)/?$ groups/view_update.php?pid=$1 [NC,QSA,L]
RewriteRule ^(.+)$ groups/index.php?gname=$1 [L,QSA]
Related
My htaccess code for URL rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^compare/(.*)/?$ compare.php?page=$1 [L,QSA]
RewriteRule ^compare/(.*)/(.*)?$ compare.php?page=$1&location=$2 [L,QSA]
This rule works for :
www.example.com/compare/car
But it is not working for:
www.example.com/compare/car/india
I want to modify the rewrite rule which works for these urls:
www.example.com/compare/car
www.example.com/compare/car/india
Is it possible? How can I modify my rewrite rule to achieve this?
There are 2 problems with your code:
.* in first rule is too greedy which matches a / so first rule is also matching /compare/car/india as well as /compare/car
Since your URI is starting with /compare and rule is rewriting to compare.php you need to turn off content negotiation by turning off MultiViews.
Your last rule is without any RewriteCond as that is only applicable to next immediate RewriteRule directive.
You may use this code in your site root .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /
# skip all files and directories from rewrite
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# one path element after compare
RewriteRule ^compare/([^/]+)/?$ compare.php?page=$1 [L,QSA,NC]
# two path elements after compare
RewriteRule ^compare/([^/]+)/([^/]+)/?$ compare.php?page=$1&location=$2 [L,QSA,NC]
So my url is like this: http://localhost/gender/?name=robert
What I wanted to do is to remove the name value pair and make it just like http://localhost/gender/robert
I googled and got this one https://stackoverflow.com/questions/21523311/removing-get-parameter-in-htaccess but it seems to complicated for me.
Any help would be much appreciated.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?name=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
</IfModule>
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^robert$ gender/?name=robert&%{QUERY_STRING} [NC,L]
If you want this to work for all pages i.e. /any-page gets served as index.php?page=any-page then use
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ gender/?name=$1&%{QUERY_STRING} [NC,L]
How do these rules work?
A RewriteRule has the following syntax
RewriteRule [Pattern] [Substitution] [Flags]
The Pattern can use a regular expression and is matched against the part of the URL after the hostname and port (with the .htaccess placed in the root dir), but before any query string.
I'm trying to get a my original url which would have been properties.php?bed=1&page=1&perpage=12&area=testarea to /search/1/1/12/testarea. But the last query string 'area' is optional and so could be blank.
I have this so far below, the problem is if I add an extra ([^/]+)/ such and
RewriteRule search/([^/]+)/([^/]+)/([^/]+)/([^/]+)/? pages/properties.php?bed=$1&page=$2&perpage=$3&area=$4
and then don't supply an area in the url, it fails to work correctly. Is there a correct way to do this where the query sting can be optional?
Also another problem I'm having is I would like this
RewriteRule search-rooms/([^/]+)/([^/]+)/([^/]+)/? pages/rooms.php?bed=$1&page=$2&perpage=$3&area=$4
to be /search/rooms/ not /search-rooms/ but if I put an extra / after search it again doesn't work correctly and the page reads the wrong data from the URL.
This is the full version
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule property/([^/]+)/? pages/show.php?id=$1 [NC,L]
RewriteRule search/([^/]+)/([^/]+)/([^/]+)/? pages/properties.php?bed=$1&page=$2&perpage=$3&area=$4 [L,QSA,NC]
RewriteRule search-rooms/([^/]+)/([^/]+)/([^/]+)/? pages/rooms.php?bed=$1&page=$2&perpage=$3&area=$4 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^www.studentlettingsagency.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^studentlettingsagency.co.uk$
RewriteRule ^(.*) http://www.stla.co.uk/$1 [QSA,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
Any help would be great!
You can use the following rule :
RewriteCond %{REQUEST_URI} !^/search/rooms
RewriteRule search/([^/]+)/([^/]+)/([^/]+)/?([^/]*)?/?$ pages/properties.php?bed=$1&page=$2&perpage=$3&area=$4 [NC,L]
RewriteRule search/rooms/([^/]+)/([^/]+)/([^/]+)/?([^/]*)?/?$ pages/rooms.php?bed=$1&page=$2&perpage=$3&area=$4 [NC,L]
RewriteCond is important here ,as it tells the first rule not to match the uri /search/rooms
Im trying to access two of my pages and rewrite their urls with .htaccess.
First page I'm trying to access:
http://api.aotikbot.tv/polls.php?username=aotik
Second page I'm trying to access:
http://api.aotikbot.tv/getpoll.php?poll=917529
My current .htaccess code:
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ polls.php?username=$1 [NC,L]
RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ getpoll.php?poll=$1 [NC,L]
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html
# End of Apache Rewrite Rules
</IfModule>
The first page works as /polls/username but the second doesn't read the poll value and I think redirects to polls.php.
Could anyone explain what I'm doing wrong?
Thanks in advance
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^[A-Za-z0-9-]+/([0-9-]+)/?$ getpoll.php?poll=$1 [NC,L]
RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ polls.php?username=$1 [NC,L]
Here, I've swapped your two rules around, so we check for IDs first (for getpoll - note how I have removed letters from the expression). If we're not doing an ID check (ie, there are letters in the segment), then it'll rewrite for polls.
Update:
Per arco444, if the username is a number, this wouldn't work. This is a more specific solution:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^getpoll/([0-9-]+)/?$ getpoll.php?poll=$1 [NC,L]
RewriteRule ^polls/([A-Za-z0-9-]+)/?$ polls.php?username=$1 [NC,L]
Ok so I have this .htaccess file and it fetches the first dir after base as $1 and the second as $2
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 ^[^\.]+[^/]$
RewriteRule (.*) http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?sub=$1&page=$2 [L]
RewriteRule ^([^/]+)/$ index.php?sub=$1&page=$1 [L]
#RewriteRule ^(.*)/(.*)$ index.php?sub=$1&page=$2 [L]
#RewriteRule ^(.*)$ index.php?sub=$1&page=$1 [L]
</IfModule>
Now we need to test this website on the productionserver in a subdirectory. This would add an additional directory to url
so:
hxxp://localhost/newwebsite/var1/var2/
Is there a way to only fetch the last two parameters of an url or should I just thrust it is going to work the way we want it. (not really an option)
Thanks!
Put the htaccess file in the newwebsite directory and change the RewriteBase / to RewriteBase /newwebsite/.