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]
Related
So, I'm currently building a REST API in PHP.
I managed to get slugs working for the most part.
If I request /api/admin/v1/users/1, it will return the user I need.
However, I also need to be able to add to it, e.g. /api/admin/v1/users/1/keys.
The HTACCESS file managing the slug is in the folder itself (/users/).
RewriteEngine On
RewriteRule ^(.*)$ user.php?slug=$1 [L]
I tried adding another line, but I think I messed up (I'm not that advanced with HTACCESS)
RewriteRule ^(.*)/keys$ keys.php?slug=$1 [L]
This didn't do anything, it still returns the user object.
RewriteRule ^(.*)$ user.php?slug=$1 [L]
RewriteRule ^(.*)/keys$ keys.php?slug=$1 [L]
The first rule matches everything, so the second rule is never processed. But since the first rule matches everything it will also rewrite itself (to user.php?slug=user.php) on the second pass by the rewrite engine.
You can resolve these issues by making the regex more restrictive. From your example URL it looks like the slug is numeric - in which case you can restrict the regex to match digits (0-9) only.
For example:
RewriteRule ^(\d*)$ user.php?slug=$1 [L]
RewriteRule ^(\d+)/keys$ keys.php?slug=$1 [L]
Note that the first rule also matches an empty URL-path, ie. no slug at all (as does your original rule). The second rule does not permit an empty slug (it would never match anyway).
The second rule don't work because the L flag stay for: last - stop processing rules
So you need to edit to:
RewriteEngine On
RewriteRule ^(.*)$ user.php?slug=$1 [QSA, L]
RewriteRule ^(.*)/keys$ keys.php?slug=$1 [QSA, L]
I have 3 step in one php file:
This is my htacess now:
RewriteRule ^igra/(.*)$ "/index.php?page=igra&id=$1"
RewriteRule ^igra/(.*)/sezona/(.*)$ "/index.php?page=igra&id=$1&season=$2"
RewriteRule ^igra/(.*)/sezona/(.*)/liga/(.*)$ "/index.php?page=igra&id=$1&season=$2&league=$3"
When i go in browser something like index.php?page=igra&id=$1 or index.php?page=igra&id=$1&season=$2 or index.php?page=igra&id=$1&season=$2&league=$3 sure with real values it works fine, but when i try to access with this pretty links it always show me the first rewrite rule..
I hope u understand me what i need here, best regards..
Your first rule is capturing everything, so the subsequent rules never get executed. Just switch them around:
RewriteRule ^igra/(.*)/sezona/(.*)/liga/(.*)$ /index.php?page=igra&id=$1&season=$2&league=$3 [L]
RewriteRule ^igra/(.*)/sezona/(.*)$ /index.php?page=igra&id=$1&season=$2 [L]
RewriteRule ^igra/(.*)$ /index.php?page=igra&id=$1 [L]
Notice also the addition of the L flag.
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.
I'm trying to figure out how to rewrite urls using apache webserver and php.
The url below is the real nonrewritten url:
http://localhost:1337/rewritetest/index.php?id=12
And I want to reach it by
http://localhost:1337/rewritetest/index/12
My indexfile looks like this:
<?php
echo $_GET['id'];
?>
Is this possible? The "new" url doesn't include any parameter names so I guess I have to use an order of parameters instead but I dont know how to reach them in that case.
Below is as far I've come with my rewrite:
RewriteCond %{QUERY_STRING} id=([-a-zA-Z0-9_+]+)
RewriteRule ^/?index.php$ %1? [R=301,L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?id=$1 [L]
Anyone have an idea of what I'm doing wrong?
it's located in the same folder as index.php
So, given the .htaccess file is located at /rewritetest/.htaccess (as opposed to the document root ie. /.htaccess) then...
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?id=$1 [L]
If you request a URL of the form /rewritetest/index/12 then the above RewriteRule pattern won't actually match anything. It tries to match "index/12", but your pattern does not contain a slash so will fail. (Is the + inside the character class intentional?)
Try something like the following instead:
RewriteRule ^(index)/(\d+)$ $1.php?id=$2 [L]
This obviously specifically matches "index" in the URL. If you are always rewriting to index.php then you don't really need "index" in the URL - unless this means something different? This also assumes that the valuue of the id parameter consists only of digits.
To rewrite the more general .../<controller>/26 to .../<controller>.php?id=26 (as mentioned comments) then try something like:
RewriteRule ^([\w-]+)/(\d+)$ $1.php?id=$2 [L]
In per-directory .htaccess files the slash prefix is omitted on the URL-path that is matched by the RewriteRule pattern, so /? is not required. The above pattern also matches something for for the id, not anything. So, /index/ would not match.
If this is a new site then the "redirect" (from /index.php?id=12 back to /index/12) is not necessarily required. That's only really required if you are changing the URL structure on an existing site where old URLs already have inbound links and are indexed by search engines. In which case you could do something like the following before the internal rewrite:
RewriteBase /rewritetest/
RewriteRule %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)
RewriteRule ^(index)\.php$ $1/%1 [R,L]
Or, for a more generic .../<controller>/26 to .../<controller>.php?id=26 (as above) then change the RewriteRule to:
RewriteRule ^([\w-]+)\.php$ $1/%1 [R,L]
The additional check against the REDIRECT_STATUS environment variable is to prevent a rewrite loop after having rewritten the URL to /index.php?id=12 earlier.
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]