I have the following page:
http://localhost/?news=19&page=2
I want to rewrite this so that it goes kind of as follows
http://localhost/news/19/page/2
This is my .htaccess file, but my code dont work :(
RewriteEngine ON
RewriteBase /
RewriteRule ^page/(.*)$ /?page=$1 [L]
RewriteRule ^news/(.*)$ /?news=$1 [L]
RewriteRule ^news/(.*)/page/(.*)$ /?news=$1&page=$2 [L]
You should specify more precicely what the allowed type of character is and you should make sure your previous rules don't cause your later rules to be ignored.
So you should put your most specific rule first and if you want digits for your news- and page ID's, you should use for example:
RewriteRule ^page/(.*)$ /?page=$1 [L]
RewriteRule ^news/(\d*)/page/(\d*)$ /?news=$1&page=$2 [L]
^^ just a digit instead of any character `.`
RewriteRule ^news/(.*)$ /?news=$1 [L]
Related
Please consider the content in my .htaccess:
##
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
## Allow a few SEO Files direct access.
RewriteRule ^robots.txt?$ robots.txt [L]
RewriteRule ^ads.txt?$ ads.txt [L]
RewriteRule ^sellers.json?$ sellers.json [L]
## Avoid rewriting rules for the admin section
RewriteRule ^(admin|resources)($|/) - [L]
## Set Ajax Request File
RewriteRule ^(kahuk-ajax)/?$ kahuk-ajax.php? [L,QSA]
## Set controller with id
RewriteRule ^([^/]+)/([0-9]+)/?$ index.php?con=$1&id=$2 [L,QSA]
## Set controller with slug
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?con=$1&slug=$2 [L,QSA]
## For paging
RewriteRule ^([^/]+)/page/([0-9]+)/?$ index.php?con=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/page/([0-9]+)/?$ index.php?con=$1&slug=$2&page=$3 [L,QSA]
## Set controller for only one parameter
RewriteRule ^page/([^/]+)/?$ index.php?con=page&slug=$1 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]
## Set home page
RewriteRule ^/?$ index.php?con=home [L]
Whenever I try to browse http://example.com/kahuk-ajax/?prefix=manage-story-vote, this opened the index.php instead of kahuk-ajax.php!
What am I doing wrong?
## Set Ajax Request File
RewriteRule ^(kahuk-ajax)/?$ kahuk-ajax.php? [L,QSA]
:
## Set controller for only one parameter
:
RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]
The request is first rewritten to kakuk-ajax.php by the first rule, but the second to last rule then rewrites this to index.php during the second pass by the rewrite engine.
You need to prevent that second rule from rewriting requests of the form kakuk-ajax.php. If these URLs do not contain dots in the first path segment (that ordinarily delimits the file extension) then you could simply include a dot in the negated character class. For example:
## Set controller for only one parameter
:
RewriteRule ^([^/.]+)/?$ index.php?con=$1 [L,QSA]
Alternatively, exclude specific file extensions:
RewriteCond %{REQUEST_URI} \.(txt|json|php)$
RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]
Or, any URL that looks like it has a file extension:
RewriteCond %{REQUEST_URI} \.\w{2,5}$
RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]
Aside:
RewriteRule ^(kahuk-ajax)/?$ kahuk-ajax.php? [L,QSA]
This rule is a little contradictory. You are removing the query string with the trailing ?, but then appending it again with the QSA flag. If you wish to preserve the query string then you don't need either. And since you are capturing the URL-path, you might as well make use of this in the substitution string. For example:
RewriteRule ^(kahuk-ajax)/?$ $1.php [L]
Also, be wary of allowing an optional trailing slash here. This promotes duplicate content which could potentially cause SEO issues. Consider redirecting one to the other instead.
## Allow a few SEO Files direct access.
RewriteRule ^robots.txt?$ robots.txt [L]
RewriteRule ^ads.txt?$ ads.txt [L]
RewriteRule ^sellers.json?$ sellers.json [L]
The ? at the end of the regex makes the preceding character (t and n in the above examples) optional, which does not really make sense here. Also, you don't need to rewrite to the same file - you don't want any rewrite/substitution to occur here. So, the above is the same as:
RewriteRule ^(robots\.txt|ads\.txt|sellers.json)$ - [L]
The - (hyphen) explicitly indicates "no substitution".
However, having included a "dot" in the (negated character class) directive above, this directive is redundant, except to "fail early".
I currently use $_GET['base'] to determine which homepage that the user visits.
This results in localhost/?base=administrator or localhost/?base=guest
I am also using this to control which page is the user at, such as
localhost/?base=guest&page=register
Is there any way to use mod_rewrite, or htaccess, to change how this system works?
Modifying my code is not an issue, is this possible?
EDIT:
I am trying to achive this:
localhost/?base=guest to localhost/guest
localhost/?base=admin to localhost/admin
localhost/?base=guest&page=register to localhost/guest/register
Below is my htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
Will the document path affect how it is being called? As I am using a case loop to include which items are needed.
This, however, works for localhost, but it will loop every other address to main.
RewriteEngine On
RewriteRule ^$ /index.php?base=guest[L]
But did not give a result as expected.
Your rules in .htaccess need to be in reverse order, like below:
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
That is because if it is kept in the order you have it, both localhost/?base=guest&page=register & localhost/?base=administrator will match the rule RewriteRule ^([^/]*)$ /?base=$1.
Having them in reverse order ensures that the first rule is matched only for localhost/?base=guest&page=register. It won't match the first rule for localhost/?base=administrator. I hope that helps.
You need to exclude your existent files and folders from the rule
RewriteEngine On
# if the request is a dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
# or file
RewriteCond %{REQUEST_FILENAME} -f
#do nothing
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
So you can use this simple code:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?base=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?base=$1&page=$2 [L]
\w will match symbols a-z, 0-9 and underscore _, I think those characters are enough for your case, but if you need expansion it will be easy
Also in this case you don't need to change your code, because you still get base and page parameters in the $_GET array
UPDATE:
to disable query string params page and base (other params may be needed) add these two lines to the code at the bottom:
RewriteCond %{THE_REQUEST} (\?|&)(page|base) [NC]
RewriteRule .* - [L,R=404]
www.example.com/category/sub_category/Having problem with the rewririte rules in .htaccess file.
my current .htaccess file looks like this.
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [NC,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [NC,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [NC,L]
RewriteRule ^([0-9]+)$ gallery.php?id=$1 [NC,L]
I'm trying create urls like the following.
www.example.com/product_name/1
www.example.com/category/sub_category/21
www.example.com/category/sub_category/product_name/233
www.example.com/gallery/872
www.example.com/gallery/872 is redirecting to www.example.com/category/sub_category/872 instead of gallery.php?id=872
edit:corrected url from www.example.com/gallery/872 to www.example.com/category/sub_category/872.
Your issue is that the first rule matches, the last one can never get applied...
RewriteEngine on
RewriteRule ^gallery/([0-9]+)/?$ gallery.php?id=$1 [NC,L]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [NC,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [NC,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [NC,L]
Rule of thumb: first the specific exceptions, then the more general rules.
The NC flag does not make sense if you also specify both, lower and upper case letters in your regex patterns. It is either/or, not and.
(note: I also included the correction #anubhava posted in his answer)
Your last rule will need regex modification since you're matching /gallery/872 and your pattern is only matching 1 or more digits.
RewriteRule ^gallery/([0-9]+)/?$ gallery.php?id=$1 [NC,L,QSA]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [QSA,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [QSA,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [QSA,L]
Also you need to recorder your rules like I showed above.
I am having issue passing get variables.
index?p=calendar refers to calendar.php located in pages/calendar.php and index.php is in root.
my URL is localhost/researchportal/calendar/11/2011
Calendar has 2 get variables, month and year. i.e calendar.php?month=11&year=11
here is my rule, but its not working.
RewriteRule ^calendar/([0-9]+)$/([0-9]+)$ index.php?p=calendar&month=$1&year=$2 [L]
I also tried
RewriteRule ^calendar/([0-9]+)$/([0-9]+)$ pages/calendar.php?month=$1&year=$2 [L]
.htaccess file
RewriteEngine On
RewriteBase /researchportal/
RewriteRule ^/calendar/([0-9]+)$ index.php?p=calendar [QSA,L]
RewriteRule ^users/login /researchportal/pages/login.php [L]
RewriteRule ^users/logout /researchportal/pages/logout.php [L]
RewriteRule ^users/register logout.php [L]
RewriteRule ^profile/([0-9]+)$ index.php?p=profile&usr_id=$1 [QSA,L]
RewriteRule ^profile/edit/([0-9]+)$ index.php?p=edit&usr_id=$1 [L]
RewriteRule ^([A-Za-z0-9-_]+)$ index.php?p=$1 [L]
^calendar/([0-9]+)$/([0-9]+)$
Why are you ending ( $ ) it twice?
^ starts and expression and $ ends it. There is no reason to have neither in the middle of an expression.
RewriteRule ^calendar/([0-9]+)/([0-9]+)$ pages/calendar.php?month=$1&year=$2 [L]
Should work better. If you for some reason would also want some query variables in the url, you can replace [L] with [L,QSA] aswell.
I think the problem is because you have an additional $ character.
RewriteRule ^calendar/([0-9]+) *$* /([0-9]+)$ index.php?p=calendar&month=$1&year=$2 [L]
There are two kind of pages -
www.mysite.com/about.php
www.mysite.com/winners.php?y=2008
I want to make them like -
1. www.mysite.com/about
2. www.mysite.com/winners/2008
my htaccess is like -
RewriteEngine On
#removing .php ext(type-1)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#for type-2 (with parameter)
RewriteRule winners/([0-9]+)/?$ winners.php?y=$1 [L]
RewriteRule winning-photograph/([0-9]+)/?$ winning-photograph.php?y=$1 [L]
RewriteRule award-giving/([0-9]+)/?$ award-giving.php?y=$1 [L]
RewriteRule album-launch/([0-9]+)/?$ album-launch.php?y=$1 [L]
RewriteRule district-fest/([0-9]+)/?$ district-fest.php?y=$1 [L]
RewriteRule lifetime-achievement-awards/([0-9]+)/?$ lifetime-achievement-awards.php?y=$1 [L]
RewriteRule critics-award-for-tv/([0-9]+)/?$ critics-award-for-tv.php?y=$1 [L]
RewriteRule photography-book/([0-9]+)/?$ photography-book.php?y=$1 [L]
The two section of this htaccess are not working at the same time. what can I do? also - is there a way to simplify the 2nd section?
Your first RewriteRule is basically preventing all the other rules from executing. It does that rule for both types of your URLs, then the [L] flag stops it from evaluating the other rules.
I would say put that rule at the very end. Do all the more-specific ones first, then have that as a generic one for all other cases, only to be evaluated if the rest of the rules didn't match the current URL.