.htaccess with php hidden parameters and language selection - php

I can't seem to get my .htaccess file to route the urls to my site correctly. I have a number of languages people can choose from wanting URL's like:
http://www.domain.com/en/
http://www.domain.com/en/contact
But I can't seem to get the page 'contact' working when writing a rule to get the 'en' variable.
RewriteRule /([^/]+)/([0-9]+)/ index.php?language=$1
I use that to grab the language code but how could I get the contact page to work?
EDIT:
Apparently I needed some QSA option but now the language get variable grabs contact as the variable with the en
RewriteRule ^(.*)$ index.php?language=$1 [QSA,L]
With this rule the site:
http://www.domain.com/en/contact
Returns:
en/contact
EDIT2
What I am trying to accomplish is the directory structure:
/
/contact
/about
Having these folders in the root but grabbing and ignoring the /en/ language variable. So I don't need a second variable for &page=contact, I need it to route into the directory folder.

Try combining your two expressions, although you need to modify the second group - [0-9]+ will only match numbers, not words like contact. Try this:
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?language=$1 [QSA,L]
The QSA option allows a query string to be appended to the clean URL, perhaps something like this:
http://www.domain.com/en/contact?to=support&subject=Hello
In response to your comment, this expression should do the trick:
RewriteRule ^([^/]*)/([^/]+)/?$ $2/index.php?language=$1 [QSA,L]
In the rewritten rule, $2 holds contact, for example, and $1 holds en. The former is used as the directory, and the latter as an argument in the query string.

Related

Simple pretty urls with .htaccess

I have a one page blog php website.
Content is dynamicaly loaded based on get parameters. I would like to use my htaccess to make pretty urls. I have these urls:
website.com/index.php?category=review&page=1
And I would like to have this:
website.com/category/review/page/1
And I also use article as get parameter. So I would like to change this:
website.com/index.php?article=12345-name-of-article
To this:
website.com/article/12345-name-of-article
I am totally new to htaccess, so any help would be appreciated.
I tried this rewrite rule:
RewriteRule ^article/([0-9a-zA-Z_-]+)$ index.php?article=$i [NC,L].
It worked somehow, but php script does not recognize url parameters. So it does not work.
Thank you very much!
You need to use QSA - When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined. :
RewriteEngine On
RewriteRule ^article/([\w-]+)(?:\.html|/)?$ index.php?article=$1 [NC,QSA,L]
RewriteRule ^category/([\w-]+)/page/([\w-]+)(?:\.html|/)?$ index.php?category=$1&page=$2 [NC,QSA,L]

$_GET variable is not being set because of .htaccess file changes

I have been using '.htaccess' to rewrite how the page url should look so instead of:-
details.php?video=how+to+do+this&user=xxx
It should be more like this:-
/details/xxx/how+to+do+this
It's working and all, but here comes the issue; when I try to add a new $_GET category that wouldn't be useful all the time, that is the "page" get variable as not all video pages are going to have this variable. So when I add this variable nothing is set, it does show in the URL however.
/details/xxx/how+to+do+this?page=2
Here is the actual line of code that I used to rewrite one of the pages that's facing this issue.
RewriteRule ^user/(.*)/(.*)$ user.php?user=$1&view=$2
You can use:
RewriteRule ^user/(.*)/(.*)$ user.php?user=$1&view=$2 [NC,L,QSA]
QSA|qsappend When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query
string, and replace it with the newly generated one. Using the [QSA]
flag causes the query strings to be combined.
https://httpd.apache.org/docs/current/en/rewrite/flags.html

How to hide argument names on address bar and replace them with a slash?

For example, I have an address like: "www.example.com/popular.php?show=comments&id=1234" and I want to show it like "www.example.com/popular/comments/1234/". What should I do?
Depending on your technology you can use .htaccess(apache) or httpd.conf(iis).
In .htaccess you would do something like this:
RewriteEngine On
RewriteRule ^popular/([^/]*)/([^/]*)$ /popular.php?show=$1&id=$2 [L]
What this does is is rewrite the requested url to the url with query variables. That is, if someone types in www.example.com/popular/comments/1234 then the server sees the url www.example.com/popular.php?show=comments&id=1234
The anatomy of the rewrite rule is this:
^popular matches any resource that begins with "/popular"
([^/]*) matches any thing between the 2 '/' characters, the first one is assigned to $1 and the second is assigned to $2.
Note, this does not redirect people who type in the old url to the new url. It only makes the new url function properly.

Redirect if URL match defined strings using .htaccess

I want to redirect a if URL match defined strings using .htaccess in a single line code
If URL is
http://example.com/mobiles/xyz/
http://example.com/computers/xyz/
http://example.com/books/xyz/
Redirect URL
http://example.com/comingsoon/
I tried this but i want to define string (e.g. books, mobiles, books) instead of (.*)
RewriteRule ^(.*)/xyz/?$ upcoming/index.html [NC,L]
Don't know if i've understand correctly the question, but this should do the trick:
RewriteRule ^mobiles|books|computers/xyz/?$ upcoming/index.html [NC,L]
Tested on https://regex101.com/

URL rewriting is not working

I would like to use URL rewriting to a website.I had placed an .htaccess file in the server and turned on rewrite mode on and it seems to be working except one issue that I'm having.
I have two php extension files namely category.php and products.php resp.
Here is my requirement, the category.php should be called when one condition is met and products.php should be called another condition is meet
.HTACCESS:
RewriteRule ([A-Za-z-_]+) category.php?arg=name
RewriteRule ^([A-Za-z-_'']+)/([0-9]+) product.php?arg=name&pid=id
So her are my website url
FOR CATEGORY PAGES
http://www.mysite.com/category1
FOR PRODUCT PAGES
http://www.mysite.com/product-name/101
So the problem is with second url rewrite condition i.e product page url.When i put the ur l in browser it goes to 404.Whereas 1 rewrite condition seemed to work.Please help to access the webpage in the above format.
Problem is that in regex's character class hyphen should be either or start or at end otherwise it needs to be escaped (it will represent range otherwise).
Both of our rules:
RewriteRule ([A-Za-z-_]+) category.php?arg=name
RewriteRule ^([A-Za-z-_'']+)/([0-9]+) product.php?arg=name&pid=id
are not using correct regex and will produce wrong results.
Replace your rules with this code:
RewriteRule ^([a-z_-]+)/?$ /category.php?arg=$1 [L,NC,QSA]
RewriteRule ^([a-z_'-]+)/([0-9]+)/?$ /product.php?arg=$1&pid=$2 [L,NC,QSA]
PS: I have made some more corrections in the rule to handle unexpected situations better.
You need to add boundaries to your regex and backreference your captured groupings:
RewriteRule ^([A-Za-z-_]+)$ category.php?arg=$1 [L]
RewriteRule ^([A-Za-z-_'']+)/([0-9]+)$ product.php?arg=$1&pid=$2 [L]

Categories