Enhance this RewriteRule - php

I'm trying to write content of .htaccess such a way that do the following conversion:
www.abc.com/page.php?id=1 to www.abc.com/page/1
So I tried to use the following RewriteRule.
RewriteRule ^page/([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
I'm wondering, If page.php needs more that 1 PHP request, for instance, id and pagenum or even 3 or more, what is the best way to implement this (while using BeautifulURL)?
I tried to use www.abc.com/page/1?pagenum=5 but it does NOT working, as you know.

With a URL like: www.abc.com/page/1?pagenum=5, you need the QSA flag:
RewriteRule ^page/([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L,QSA]
or else the pagenum=5 won't get added. Additionally, you can have the URL look (optionally) like this:
http://www.abc.com/page/1/5
for: /page.php?id=1&pagenum=5 using this rule:
RewriteRule ^page/([A-Za-z0-9-]+)/([0-9]+)/?$ page.php?id=$1&pagenum=$2 [L,NC]
You'd need both rules if you want both to work, with the extra /5 at the end and without it.

Related

How to rewrite specific URL using htaccess

I've been breaking my head on this for quite some time and I don't see the solution.
I want to rewrite a URL with a GET language parameter to a more clean URL.
For instance:
http://www.example.com?lang=en
Needs to be:
http://www.example.com/en
The above works fine with this rewrite rule:
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L]
But I can't get it to work on URLs like these:
http://www.example.com/contact.php?lang=en
http://www.example.com/about.php?lang=en
That need to be:
http://www.example.com/en/contact.php
http://www.example.com/en/about.php
Anyone have an idea what I'm missing in my rewrite rule to make this work?
You will need an additional rewrite rule for handling /en/about.php:
RewriteEngine On
RewriteRule ^(en|nl|fr|de)/([\w-]+\.php)$ $2?lang=$1 [L,QSA]
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L,QSA]

how to rewrite html url as directory in htaccess

I want to rewrite all of my category urls as directories instead of long html pages. How do I do that? (The site is homewetbar.com if you need to look at the directory structure further to understand how it currently works.)
For Example:
First level category:
www.site.com/great-gift-ideas-c-35.html
I would like to display instead as:
www.site.com/great-gift-ideas-c-35
Second level category:
www.site.com/great-gift-ideas-gifts-recipient-c-35_85.html
I would like to display instead as:
www.site.com/great-gift-ideas/gifts-recipient-c-35_85
Third level category:
www.site.com/gifts-recipient/groomsmen-gifts-c-35_85_43.html
I would like to display instead as:
www.site.com/gifts-recipient/groomsmen-gifts-c-35_85_43
OR:
www.site.com/great-gift-ideas/gifts-recipient/groomsmen-gifts-c-35_85_43 (whichever is easier)
I think mod_rewrite is what you are looking for, here is a link with how to set it up and how to start rewriting your urls. It should have enough information for what you are looking for, for more advanced stuff you will want to know more about regular expressions, but your examples are simple enough.
RewriteEngine on
RewriteRule ^great-gift-ideas-c-35.html$ great-gift-ideas-c-35
this should work for your first example, the rest just replace out the appropriate urls.
You would need to rewrite all the href in your site to delete the ".html" part. Then, add this to your .htaccess
RewriteEngine on
RewriteCond ${REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html [L]
That would make any URL without the ".html" work as if it had it
Just check out this site: http://www.generateit.net/mod-rewrite/index.php it generate all you need
RewriteEngine On
RewriteRule ^([^/]*)$ /?example=$1 [L]
Changing example to what you need should do it

Add $_GET-vars to a rewritten URL

I have several URLs which are dealt with by rewrite rules. Basically I want to add some $_GET-vars to a rewritten URL.
Say I have:
http://www.domain.com/search/the-netherlands/overijssel/
I would like to add some extra data to the URL like:
http://www.domain.com/search/the-netherlands/overijssel/?custom_var=1
My RewriteRule looks like this:
RewriteEngine On
RewriteRule ^search/(.*)/(.*)/$ /index.php?page=Search&Country=$1&Region=$2 [L]
When I var_dump the contents of $_GET I don't see custom_var anywhere. Can this be done, using the existing rewrite rule?
Since your query already has some parameters, append [QSA] to the end of the RewriteRule.
RewriteRule ^search/(.*)/(.*)/$ /index.php?page=Search&Country=$1&Region=$2 [QSA,L]

how to retrieve a specific get data in htaccess and pass it on rewrite rule

I have a single get data (affid) that i want to retrieve using .htaccess but i don't know how to do it. Can you help me out?
Here is an example of links:
mysite.com/searchmembers?affid=1001
mysite.com/profle/123?affid=1002
mysite.com/videos/567?affid=1003
Another thing that might give a problem is these links already have been rewritten on .htaccess
RewriteRule ^searchmembers? index.php?task=searchMembers
RewriteRule ^profile/(.*)? index.php?task=viewProfile&id=$1
RewriteRule ^videos? index.php?task=videos&id=$1
i just want to retrieve the affid and add it to the links like this:
RewriteRule ^searchmembers... index.php?task=searchMembers&affid=(data retrieved)
RewriteRule ^profile/(.*)... index.php?task=viewProfile&id=$1&affid=(data retrieved)
RewriteRule ^videos? index.php... task=videos&id=$1&affid=(data retrieved)
i know i could add it on htaccess for each of these links but if there is an easier way to do this then it would be a great help. thank you for any response that i will receive!
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^searchmembers$ index.php?task=searchMembers [QSA,NC,L]
RewriteRule ^profile/(.*)$ index.php?task=viewProfile&id=$1 [QSA,NC,L]
RewriteRule ^videos/(.*)$ index.php?task=videos&id=$1 [QSA,NC,L]
If you just need to append a new query parameter (like task here), any existing query parameters can be appended automatically using the [QSA] (Query String Append) flag. I've also corrected the regex used in your RewriteRules. The use of ? is incorrect.

SEO friendly URL not working using .htaccess

I'm trying to implement a SEO friendly URL using .htaccess by using the RewriteRule below
RewriteRule ^n/article/([a-zA-Z0-9]+)/$ article.php?title=$1
The actaul URL looks like this
http://localhost/n/article.php?title=this-is-the-first-news-article
but I want it to look like this :
http://localohst/n/article/this-is-the-first-news-article
When I applied the RewiteRule above it does not change to the desired URL
This should do it. You are missing the n. Not sure why you need the word title though.
RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1
You have to capture the query string first. You can't do that with a RewriteRule because they ignore the query string. Here we're using [R] to redirect. If this is working for you and there is the potential that the old URLs are being stored somewhere as links, then you may want to specify [R=301]. Be sure to remove all old-style links from your site though (that contain the previous link format we're rewriting), that way you're not penalized for not updating your links.
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} title=([^&]+)
RewriteRule ^n/article.php n/article/%1? [R,L]
If your site needs the formatting to function from the original, you might also need this after the first rule. This rule quietly redirects the URL back to the original without showing it to the end user:
RewriteRule ^n/article/(.*) n/article.php?title=$1 [L]
it will be RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1

Categories