simple htaccess redirect - php

It's a part of my htaccess:
RewriteRule ^post/(.*)$ post.php?name=$1
RewriteRule ^(.*)/$ cat.php?name=$1
The URI will be somthing like this:
www.domain.com/category-name/
www.domain.com/post/hello-world
as you see, in the end of the first address (category) there is a '/' and on the second there isn't '/', how can I do it too on the second address? if I will do somthing like this:
RewriteRule ^post/(.*)/$ post.php?name=$1
it won't work because the server 'thinks' that I mean to category address.
hope you understand thank you.

Use the [L] modifier on every rule so it stops processing further rules when a rule matched. In this case you can even make the trailing slash optional!
RewriteRule ^post/(.*)$ post.php?name=$1 [L]
RewriteRule ^(.*)/?$ cat.php?name=$1 [L]

Try using (.*?) instead of (.*).
Without the question mark, it's "greedy" in that it will match everything up to the final match on the line. With the question mark, it will only match up to the first match on the line.

Make the slash optional (both times):
RewriteRule ^post/(.*?)/?$ post.php?name=$1 [L]
RewriteRule ^(.*?)/?$ cat.php?name=$1 [L]

Related

Redirect rule to php query but remove any query string in the requested url

My url is being redirected using .htaccess as follows:
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
Friendly url -> translates to php url
domain.com/b/hello/2 -> b/view.php?id=2&name=hello
BUT when someone comes to the site as follows:
domain.com/b/hello/2?query=xyz
I don't know how to get rid of the ?query=xyz
I have tried everything including [QSD] and I can't seem to get it to work.
Update
I have managed to get it to work with the following but it does two 301 redirects instead of one:
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule ^b/(.*)$ /x/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)/([^/]+)?$ b/view.php?id=$2&&name=$1
Check if adding the question mark at the end of the rule will cancel appending query string from left side
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1? [QSD,L]
You can use an additional Rule to catch for GET parameters and strip them off.
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
To make it only work on the /b/ subfolder, use this:
RewriteCond %{QUERY_STRING} .+
RewriteRule ^b/(.*)$ b/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
The first rule will redirect everything that matches your rule to the URL without any GET parameters (note the ? at the end of the rewrite rule, it will strip off the parameters).
The second rule will match in the case the first rule cannot be applied, i.e., when there are no paramaters

Rewrite URL to hide ?get= in .htaccess

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]

Trouble when writing RewriteRule in apache

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]

Could i have 2 links from the same page with htaccess?

So my htaccess lines look like this:
RewriteRule ^meniu/([a-zA-Z0-9]+)/$ produse.php?categorie=$1
RewriteRule ^meniu/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ produse.php?categorie=$1&produs=$2
www.mysite.com/meniu/pizza/ works
www.mysite.com/meniu/pizza/Quatro_Formaggi/ doesn't work, it displays 404 not found.
Your URL has the underscore character
www.mysite.com/meniu/pizza/Quatro_Formaggi/
so just add the _ to the RewriteRule to match it
RewriteRule ^meniu/([a-zA-Z0-9]+)/$ produse.php?categorie=$1
RewriteRule ^meniu/([a-zA-Z0-9]+)/([a-zA-Z0-9_]+)/$ produse.php?categorie=$1&produs=$2
Your URL has a - (underscore character); and your rules don't; so you need to add the _ to the rule.
Also instead of using [a-zA-Z0-9] I would suggest using [a-z0-9] and the Not Case Sensitive flag ([NC]). So My suggested rules would be:
RewriteRule ^meniu/([a-z0-9]+)/$ produse.php?categorie=$1 [NC]
RewriteRule ^meniu/([a-z0-9]+)/([a-z0-9_]+)/$ produse.php?categorie=$1&produs=$2 [NC]
Also make sure you have a rule to add trailing slashes above this one or it will be annoying to any users hand entering the address to remember to have the trailing slash.

Url Rewriting regex

I have this rule in my .htaccess
RewriteRule ^([^/\.]+)/?$ ?page=user&id=$1 [L]
It rewrite a url like
http://sitename.ext/nickname
to
http://sitename.ext/?page=user&nickname
The problem is that with a url with dots like http://sitename.ext/nick.name.test i get a 404 error..
I'm not good with regex..
That's because it's not being rewritten. You specificaly told it to exclude ., and that's what it's doing.
Personally, I would favour something like this:
RewriteRule user/(.+) ?page=user&id=$1 [L]
If you want to match any character except a slash, the regex is [^/], since the \. will cause it also to not match dots.
Your rule should be
RewriteRule ^([^/]+)/?$ ?page=user&id=$1 [L]
You might find this site helpful.

Categories