I inserted this rule in my .htaccess file:
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
In this way I can get friendly urls like this:
http://localhost/mysite/london
I'd also like to use a friendly url for my contact page like so:
https://localhost/mysite/index.php?content=message to become:
https://localhost/mysite/contact
But if I insert the below rule into .htaccess...
RewriteRule ^contact/?$ index.php?content=message [NC,L]
...it doesn't work as it seems that the rule for the categories affects this rule.
In fact, if I comment out the category rule...
#RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
...the url friendly rule for the contact page works (https://localhost/mysite/contact)
So I'm looking for the possibility to exclude some parameter from the category rule to allow for a redirect in some case to another url.
Thanks for any suggestions...
Firstly you need to make sure the contact rule is placed before the more generic one so htaccess can process it first:
RewriteRule ^contact/?$ index.php?content=message [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
If you have it the other way around the generic rule will always be looked at and match before htaccess even gets to the contact rule.
Note though that the way you wrote your rules it will only match URLs such as
https://exmaple.com/contact
but not
https://exmaple.com/contact/something-else
However, looking at your more generic rule I assume this is intentional.
Related
I am writing rules in my .htaccess file to configure the URL shown.
In my site I have two pages. Eg. page.php and page_edit.php.
I my .htaccess file I am making the following rules:
RewriteRule ^page page.php [NC,L]
RewriteRule ^page/create page_edit.php?editID=1 [NC,L]
However, the 2nd rule never gets caught and the page is always directed to page.php (the first rule). I guess this is becuase it is has the string for the first rule ('page') as a substring.
How can I get round this?
you can change Rule order, the more specific in top
RewriteRule ^page/create page_edit.php?editID=1 [NC,L]
RewriteRule ^page page.php [NC,L]
I have the following htaccess rule
RewriteRule ^([a-zA-Z-_0-9]+)/([a-zA-Z-_0-9]+)$ ?page=$1&action=$2 [L]
For example if I want to add something or edit something the url will be like:
www.website.com/page_name/add
And that rule is applicable for all pages except for one page named portfolio.. for this page I want first to get a category value and than add or edit stuffs.
I tried under the first rule to put this rule:
RewriteRule ^portfolio/([a-zA-Z-_0-9]+)$ ?page=portfolio&category=$1 [L]
To get a link like that:
www.website.com/portfolio/demos
Or
www.website.com/portfolio/desings
But always the first rule that works (consider the 2nd parameter like an action and not like an category).
Any idea for make exception for that specific page to treat what comes after like an "cat" and not like an "action"?
The L flag in HTACCESS tells Apache to ignore anything after that rule if the pattern is a match (which it is in your example).
Swap the rules around:
RewriteRule ^portfolio/([a-zA-Z-_0-9]+)$ ?page=???&category=$1 [L]
RewriteRule ^([a-zA-Z-_0-9]+)/([a-zA-Z-_0-9]+)$ ?page=$1&action=$2 [L]
Also, notice that in the first rule, there is no second parameter (since there's only one pattern), so I'm not sure where you intend to send the user...
First exclude portifilio from first rule so , replace this line :
RewriteCond %{REQUEST_URI} !^/portfolio
RewriteRule ^([a-zA-Z-_0-9]+)/([a-zA-Z-_0-9]+)$ ?page=$1&action=$2 [L]
Also in this rule :
RewriteRule ^portfolio/([a-zA-Z-_0-9]+)$ ?page=$1&category=$2 [L]
you will be able match against the number only not page name , it should be like this :
RewriteRule ^portfolio/([a-zA-Z-_0-9]+)$ ?page=portfolio&category=$1 [L]
OR
RewriteRule ^(whatever)/([a-zA-Z-_0-9]+)$ ?page=$1&category=$2 [L]
I've been breaking my head on this for quite some time and I don't see the solution.
I want to rewrite a URL with a GET language parameter to a more clean URL.
For instance:
http://www.example.com?lang=en
Needs to be:
http://www.example.com/en
The above works fine with this rewrite rule:
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L]
But I can't get it to work on URLs like these:
http://www.example.com/contact.php?lang=en
http://www.example.com/about.php?lang=en
That need to be:
http://www.example.com/en/contact.php
http://www.example.com/en/about.php
Anyone have an idea what I'm missing in my rewrite rule to make this work?
You will need an additional rewrite rule for handling /en/about.php:
RewriteEngine On
RewriteRule ^(en|nl|fr|de)/([\w-]+\.php)$ $2?lang=$1 [L,QSA]
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L,QSA]
Hi I have a rewrites in place to add an author name so:
example.com/author/first-name-last-name
Now when someone navigates to:
example.com/author
it shows all the authors, however I would like the URL to be
example.com/authors
With an 's' appended at the end.
How can I achieve this without redirecting to an actual page called authors.php?
My current rule is:
RewriteRule ^author/([A-Za-z0-9-]+)/?$ author.php?authorslug=$1 [NC,L]
Probably this is what you're looking for:
RewriteEngine On
# show all authors
RewriteRule ^authors/?$ /author.php [L]
# show a particular author
RewriteRule ^author/([a-z0-9-]+)/?$ author.php?authorslug=$1 [NC,L]
Add a rule before the one you already have:
RewriteRule ^author/?$ /authors.php [L]
New to htaccess: how can i write rewrite Rules for this,
instead of http://www.x.com/directory.php?state=TX have www.x.com/texas-fishing
instead of http://www.x.com/directory.php?state=WA have www.x.com/washington-fishing
Actually $1 indicates the dynamic part of the Url like(TX,WA..), but here i need complete title of the page. So do i need to modify in script or .htaccess is enough to manage.
If it is enough then how can we manage..
RewriteRule ^texas-fishing$ directory.php?state=TX [NC,L]
RewriteRule ^washington-fishing$ directory.php?state=WA [NC,L]