My current url is like news/sometopics/newstitle.html.
And I want url like this sometopics/newstitle.html.
Why is the following not working?
RewriteRule ^(.+/)(.+).html$ news/$1/$2.html
By the way, I am using GoDaddy Apache server. This rule:
RewriteRule ^search/$ admin/plus/search.php
works fine.
The first rule will match only alphanumeric strings such as: titlenews123. I prefer it over the generic match everything meta-char (.+)
RewriteRule ^([0-9a-z-]+)/([0-9a-z-]+).html$ news/$1/$2.html [L]
If your sometopics and newstitle are not only alphanumeric you can do (discouraged)
RewriteRule ^([^/]+)/([^/]+).html$ news/$1/$2.html [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 use htaccess for make mu URL clean. My original ULR is
http://localhost/bookStore/single_book.php?name=This%20is%20book%20no%204
i use php urlencode() function to make this
http://localhost/bookStore/single_book.php?name=This+is+book+no+4
but now i want to make this like:
http://localhost/bookStore/single_book/This+is+book+no+4
using htaccess
RewriteRule ^single_book/([0-9a-zA-Z_-]+) single_book.php?name=$2 [NC,L]
RewriteRule not working
RewriteCond %{QUERY_STRING} ^name=(.+)
RewriteRule ^(bookStore/single_book)(\.php)$ $1/%1?
This matches if name is in the query string and if the name is the only portion of the query string.
I tested on https://htaccess.madewithlove.be/
And for playing with the regex, i used https://regexr.com/
The trailing ? in the RewriteRule removes the query string.
This is the structure of my website:
website.com/events/beachparty?date=1224
website.com/events/poolparty?date=0101
website.com/events/boatparty?date=1105
There are lots of different pages all of which I would like to use the get feature on.
I want to rewrite the URL using htaccess so that they can be loaded like this:
website.com/events/beachparty/1224
website.com/events/poolparty/0101
website.com/events/boatparty/1105
Is this possible without having to create a separate rule for each page?
Thanks in advance!
Is this possible without having to create a separate rule for each
page?
Yes, you can use a regex, i.e.:
RewriteEngine On
RewriteRule ^events/page([0-9]+)/([0-9]+)/?$ /events/page$1?date=$2 [L]
The above will rewrite:
www.yoursite.com/events/page99/123
to
www.yoursite.com/events/page99?date=123
Notes:
([0-9]+) will match 1 or more (+) digits
The last forward slash is optional /?
$ means the end of the line (url)
[L] = Last, apache will stop processing further rules
Update based on your comments:
RewriteEngine On
RewriteRule ^events/([0-9a-zA-Z]+)/([0-9]+)/?$ /events/$1.php?date=$2 [L]
RewriteRule ^events/([0-9a-zA-Z]+)/?$ /events/$1.php [L]
[0-9a-zA-Z] - will match any digit or letter from a to z or A to Z, 1 or more times
Try...
RewriteEngine On
RewriteRule ^events/beachparty/([0-9]+)/?$ /events/beachparty?date=$1 [L]
RewriteRule ^events/poolparty/([0-9]+)/?$ /events/beachparty?date=$1 [L]
RewriteRule ^events/boatparty/([0-9]+)/?$ /events/beachparty?date=$1 [L]
I have a multi blog website in which there are multiple domains.
The thing is i need a rewrite rule for the following syntax
XXX.domain.com/{any other things} to www.domain.com/domain/XXX/{any other things}
Also i had written a set of rules for the below one too
^domain/([a-zA-Z0-9]+)/cat/([a-zA-Z0-9]+)$ index.php?domain=$1&cat=$2
So the things is i need to construct a rule which converts domain to path then again process the other things as follows with that.
Before your other rule, you'll need
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond $1 !^domain/
RewriteRule ^(.*)$ /domain/%1/$1 [L]
RewriteRule ^([a-zA-Z0-9\-_]+).domain.com/(.*)$ www.domain.com/$1/$2
([a-zA-Z0-9\-_]+) matches a subdomain.
(.*) matches everything.
This will rewrite whatever.domain.com/anything to www.domain.com/whatever/anything.
I hope I understood correctly, that your redirect example has nothing to do with the requirement:
RewriteRule ^([^.]+)\.domain\.com/(.*)$ www.domain.com/domain/$1/$2
[^.]+ captures the subdomain, the rest is selfevident, I hope.
I have created a login interface where user can register there username. Now i wanted to give each user a vanity url like, example.com/user. I am using .htaccess rewrite conditions and php for that. Everything is working fine except when i try a url like example.com/chat/xx it displays a profile page with "xx" id. Instead it should have thrown a 404 page(thats what i want). I want that vanity url thing only works if a user input "example.com/user" not in sub directory like "example.com/xyz/user". Is this possible ?
htaccess --
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ profile.php?id=$1 [L]
Php used --
if(isset($_GET['id']))
// fetching user data and displaying it
else
header(location:index.php);
Then you must match on an URL-path without slashes / only
RewriteEngine on
RewriteRule ^/?([^/\.]+)$ profile.php?id=$1 [L]
This regular expression ^([^\.]+)$ matches everything without a dot ., e.g.
a
bcd
hello/how/are/you
chat/xx
but it doesn't match
test.php
hello.world
chat/xx.bar
This one ^/?([^/\.]+)$ works the same, except it disallows slashes / too. I.e. it allows everything, except URL-paths, containing either dot . or slash /.
For more details on Apache's regular expressions, see Glossary - Regular Expression (Regex) and Rewrite Intro - Regular Expressions.