Apache Url Rewrite Query String SEO - php

I have the following rewrite.
RewriteRule ^/news/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /pagebase.php?pbid=3656&nid=$1&title=$2 [QSA,L,I]
http://www.domain.com/news/1/new-event/
So that the above url will be rewritten as:
http://www.domain.com/pagebase.php?pbid=3656&nid=1&title=new-event
This works perfectly. However I want people to be able to type in:
http://www.domain.com/news without any query strings and have it rewrite as this:
http://www.domain.com/pagebase.php?pbid=3656&nid=&title=
However the match fails so I get a 404 error.
Is there anyway I can rewrite my rule to make the last 2 query string options optional. I was able to figure it out using multiple rewrite rules and placing them in the correct order, but I'd like to get it so I can get it working with one rule.

Use two rewrite rules:
RewriteRule ^/news/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /pagebase.php?pbid=3656&nid=$1&title=$2 [QSA,L,I]
RewriteRule ^/news$ /pagebase.php?pbid=3656&nid=&title= [QSA,L,I]

Related

url search for file in subdirectory instead from public_html using htaccess

I am working on a website in which I write code for htaccess but the thing which I wanted to do is not happening. I have url which is:
http://www.example.com/demo.php?id=234&title=ask%20me%20a%20question
I converted to below url using htaccess:
http://www.example.com/234/ask%20me%a%question
htaccess code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([0-9]{4})/([a-z]+)/$ demo.php?url=$1&url2=$2
So. the problem is converted url is search for related file in subdirectory instead of server root i.e; public_html. I want to know how could this problem will solve.
Plz help me. Thanks.
The second parameter in your request requires that characters other than a-z be included, but you are limiting it to a-z.
In addition, you are requesting 234 in the URI, but checking for 4 numbers in the first parameter.
As such, change your rule to the following:
RewriteRule ^([0-9]{3,4})/([^/]+)/?$ demo.php?url=$1&url2=$2 [L]
Changes
Allow 3 or 4 numbers in the first parameter. If you want to be more flexible, you can change it to ([0-9]+).
Check for all characters other than / in the second parameter.
Make the trailing slash optional using /?.
Add the L flag to stop rewriting if the rule is matched (always good to have for when you add other rules).

URL rewriting and get parameters

I am currently building a website where the content of pages come from a database.
To construct my SQL statement, I use the parameters in the URL and until now everything worked fine.
Now I would like to rewrite the URLs to have clean URL, so by default my URLs look like:
lunettes-collection.php?supplier=all&type=vue
I wrote a rule to convert this URL like
lunettes-collection-vue.html
But now when I reach this URL I obviously cannot get the parameter type=vue.
What is the best practice to handle such situation?
With .htaccess you can do that
# Turn Rewrite Engine On
RewriteEngine on
#Rewrite lunettes-collection.php?supplier=all&type=vue to
#lunettes-collection/all/vue
RewriteRule ^lunettes-collection/([a-zA-Z]+)/([a-zA-Z]+) lunettes-collection.php?supplier=$1&type=$2 [NC,L]
Remember that URL will only support letter from a-z and A-Z if you want enable digits add an extra part 0-9
Rewrite would not be Such an outcome.
The $_GET will get normal variable.
Please show your Rewrite rules.
I try the Nginx rules is work normal like this.
rewrite ^/lunettes-collection-vue.html$ /lunettes-collection.php?supplier=all&type=vue last;

CodeIgniter Mod Rewrite Rules and the Controller

Learning PHP, I am playing around with mod_rewrite and CodeIgniter. I have configured my .htaccess file correctly with
RewriteEngine On
RewriteRule ^(resources)/(.*) $1/$2 [L]
RewriteRule ^(user_guide)/(.*) $1/$2 [L]
RewriteRule (.*) index.php?$1 [L]
I understand a bit of regex, and can appreciate what happens here. The rewrite rules are applied and the server than handles the final URL which in the above case- attaches index.php (the front controller) to the "pretty" URL. So far so good.
I now want a URL pattern :
/<person-name>/at/<place>
to get translated to :
/index.php/person/list?personName=$1&place=$2
And i handle the request at my list function in the person controller. I do not understand why the following doesn't work:
RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php/person/list?personName=$1&place=$2 [L]
What am i doing wrong/where is my understanding flawed? I see that the placeholders are extracted correctly ($1 and $3), however, it throws a CodeIgniter 404.
Many thanks!
It's possible that the simplest fix will fix your issue. By wrapping "at" in parentheses, you're creating another matching group, which means that $2 will always be "at." That could be breaking everything. You want index.php?person/list?personName=$1&place=$3 But you may have noticed that issue and fixed it without fixing the problem, in which case, read on.
Check out How to make CodeIgniter accept "query string" URLs?. It seems to indicate that you can't mix and match the segment-based approach and the query string approach. Without seeing your controller code, I can't say for certain, but I'd start investigating there. You might also try:
RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php?person/list/$1/$3 [L]
which would do the same thing the general-purpose CI redirect rule below does; send the URL off to index.php as a query string for processing. You've said you got it working with routes, so rather than passing a query string to your controller, you can expect person and place as two arguments. CI's handling of query strings leaves a lot to be desired, and I've never tried to MOD_REWRITE something including a query string into a query string.

.htaccess with php hidden parameters and language selection

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.

Detecting empty querystring with mod_rewrite

I have a strange problem, where to generate pages I am using my URL's as:
site/city/city-name/page-number
for that mod_rewrite is:
RewriteRule ^city/(.*)/(.*)$ tagcity.php?tag=$1&pnum=$2 [L]
But now the problem is if I delete the city-name and make it empty like "site/city//page-number" then it starts taking page-number as city-name.
Also can I have only one rewrite to serve pages with or without page numbers?
I know its a strange situation but unfortunatly some of my sites pages are indexed in Google as above.
How to detect that its empty so I can generate a 404 page?
Use a + instead:
RewriteRule ^city/(.+)/(.*)$ tagcity.php?tag=$1&pnum=$2 [L,QSA]
* means "0 or more repetitions", whereas + means "1 or more repetitions". Thus using a + requires that field to have at least something in it.
Also, you may want to make that [L,QSA] so that if there are any additional query parameters in the original URL, they'll be preserved. (QSA = Query String Append)
As requested in the comments, a rule with the ability to handle completely missing page number:
RewriteRule ^city/([^/]+)(?:/(.*))?$ tagcity.php?tag=$1&pnum=$2 [L,QSA]

Categories