How to obtain a customized (and friendly) url using .htaccess? - apache

Say I have a URL like http://abc.com/index.php?cat=37&subsubcat=0&subcat=199&page=product_detail&product_id=1661
Can I use .htaccess to redirect/rewrite this to a URL like http://abc.com/simplerName?
I found quite a few posts on SO that ask for friendly URLs, but I want to take it a step further, i.e. I'd like to specify what the subfolder name should be (in the above example, it's "simplerName"). Now I've got 10 URLs that I want to customize and I'm totally cool with specifying 10 rules for each URL.
But I'm not sure how to achieve this using .htaccess. Is this even possible?

Because you are using non-existing "subfiolders" to indicate which rule should match what, this is going to be pretty straight-forward:
You want something like this:
RewriteEngine On
RewriteRule ^/?category/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/ /index.php?cat=$1&subsubcat=$2&subcat=$3&page=$4&product_id=$5 [L]
RewriteRule ^/?something-else/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/ /index.php?something=$1&subsub=$2&sub=$3&page=$4&something_id=$5 [L]
etc.
You'll then need to change all the URLs that you serve to the friendly looking ones instead of the ones with the query string.

Related

Rewriting dynamic url's using apache rewrite

I have a anchor tag in my html content like this class="list-content" href="/abcd/test.html".
and this is in a lot of places in my html for a list of some results.
I need to append all these URLs that are in "href" by appending a prefix.
For example: /abcd/test.html should be dynamically changed as newprefix/abcd/test.html
If i have another one like /xyz/some.html then this should be changed as newprefix/xyz/some.html
I have explored different solutions over the internet and I have not found something that would fit my problem.
To implement an external redirect to prepend /newprefix to these requests you could do something like the following near the top of the root .htaccess (or server config).
For example:
RewriteEngine On
RewriteRule ^[^/]+/[^./]+\.html$ /newprefix/$0 [R=302,L]
The above will redirect requests for /abcd/test.html or /xyz/some.html to /newprefix/abcd/test.html and /newprefix/xyz/some.html respectively. Anything that matches the pattern /<something>/<file>.html.
$0 is a backreference that contains the URL-path that is matched by the RewriteRule pattern.
Note that this is not "url-rewriting" since you stated in comments that you do not want to "hide" the /newprefix part of the URL from your users. An external redirect is therefore the only solution if you are intending to use Apache / mod_rewrite (as tagged).
Aside: This is not particularly good for SEO, your users or your server since the user is externally redirected everytime they click one of your links, potentially doubling the number of requests that hit your server and slowing your users.

Htaccess to rewrite url for an existing php app

I have developed a PHP application and I want to rewrite the URLs to make it cleaner,
I have multiple pages, each page can have 0 or multiple GET variables,
Example of what I want to achieve:
index.php -> /
/index.php?var1=aaa&var2=bbb&var3=ccc -> /var1/aaa/var2/bbb/var3/ccc
/product.php?var4=ddd&var5=eee -> /product/var4/ddd/var5/eee
/products-list.php -> /products-list/
etc...
Can anyone help me with a full .htaccess file to achieve this result?
And is there a way to make the PHP read the GET params or do I need to add a function to create a $_GET array from the URL on each page?
Thank you.
These simple rules will rewrite the examples you give in the question:
RewriteEngine on
RewriteEngine ^/?products-list/?$ /products-list.php [QSA,END]
RewriteRule ^/?product/([^/+])/([^/+])/([^/+])/([^/+])/?$ /product.php?$1=$2&$3=$4 [QSA,END]
RewriteRule ^/?([^/+])/([^/+])/([^/+])/([^/+])/([^/+])/([^/+])/?$ /index.php?$1=$2&$3=$4&$5=$6 [QSA,END]
You might have to add some conditions to prevent interference with other rules or rewriting loops. But that is nothing we can know without insight into your specific situation and setup.

htaccess url redirects with parameters not working

I did a search of previous questions about URL redirects with parameters, but none seem to speak to my particular problem. I'm not a programmer so I don't really know how to adapt other suggestions to my situation. Specific HTACCESS strings to try (and adapt for other URLs) would really help me.
I did an SEO restructure of my WP blog permalinks and I am finding that although the naked URLs are redirecting OK, URLs with parameters are not redirecting, they are going to a 404 error. I need URL parameters because my site is multilingual (Transposh plugin) so the "lang" parameter tells the site what language to translate the content to.
I think I may need to create a bunch of HTACCESS redirects that will redirect old URLs with a language parameter to the new permalinks for those URLs and pass the lang parameter through.
An example of this would be:
Source URL: /this-old-postname/?lang=(*)
Destination URL /blog/this-new-postname/?lang=$
There's no way to predict the URL pattern (although the parameter pattern is predictable) as each URL was tweaked for best SEO contribution.
I expect I'll need to write lots of these, each unique, so if you are able to provide an example can you please provide it for two redirects which would work for the following actual examples?
Source: http://www.travelnasia.com/thailand/bangkok/don-mueang-airport/?lang=zh
Destination: http://www.travelnasia.com/thailand/don-mueang-airport-bangkok/?lang=zh
Source: http://www.travelnasia.com/blog/map-attractions-bangkok-skytrain/?lang=zh
Destination: http://www.travelnasia.com/blog/bangkok-skytrain-bts-mrt-lines/?lang=zh
MOD_REWRITE is already enabled and standard redirects created in HTACCESS do work. I am pretty sure to achieve this I will need to use:
RewriteCond %{QUERY_STRING} ^lang=(.*)$
I understand this ensures the query string is read from the source URL.
I thought a redirect rule like this should work, but it doesn't:
RewriteRule ^test-redirect/$ http://test.travelnasia.com/destination/hanoi/hanoi-vietnam-destination-guide/$1 [L,QSA]
I've tried lots of other combinations but none of them seem to work.
Thanks in advance for any help offered.
Tony
If you don't mind including other query string parameters, [QSA] (query string append) is by far your simplest option.
Make it a 301 redirect with [R=301] if this is permanent change.
This should work for your purposes
RewriteEngine on # if not already enabled
RewriteRule ^thailand/bangkok/don-mueang-airport/?$ /thailand/don-mueang-airport-bangkok/ [QSA,R=301]
RewriteRule ^/blog/map-attractions-bangkok-skytrain/?$ /blog/bangkok-skytrain-bts-mrt-lines/ [QSA,R=301]
# ...

How does URL rewriting work?

I am new to URL rewriting and I have an .htaccess file that looks like this:
RewriteEngine On
RewriteRule /*\.(css|js|gif|png|jpe?g)$ - [NC,L]
RewriteRule "^(.*)$" "www/index.php?_url=$1" [QSA,L]
Does this code just rewrite the code internally, or is it supposed to change to URL in the address bar? As of now it does not change the address bar, and I'm not really sure yet but I am thinking that I will probably want the option to do so for bookmarking purposes. So if there is a way could you please let me know or direct me to a pretty noob friendly guide on URL rewriting where I can figure it out on my own because I haven't been able to find one.
Thanks for the help!
As it stands, it will just do an internal rewrite. To redirect the user (thereby changing their address bar), add R to the flags (e.g. [NC,R,L] or [R,QSA,L])
URL rewriting is completely server-side (unless you do a redirect). The client (and thus their address bar) will not know what the server is doing with the URL.
Here's a good beginner tutorial that explains URL rewriting and goes through progressively more complex examples.

Using Apache mod_rewrite to remove sub-directories from URL

I'm managing an instance of Wordpress where the URLs are in the following format:
http://www.example.com/example-category/blog-post-permalink/
The blog author did an inconsistent job of adding categories to posts, so while some of them had legitimate categories in their URLS, at least half are "uncategorised".
I can easily change Wordpress to render the URL without the category name (e.g., http://www.example.com/blog-post-permalink/), but I'd like to create a mod_rewrite rule to automatically redirect any requests for the previous format to the new, cleaner one.
How can I use a mod_rewrite recipe to handle this, taking into account that I want to honor requests for the real WordPress directories that are in my webroot?
Something as simple as:
RewriteRule ^/[^/]+/([^/]+)/?$ /$2 [R]
Perhaps would do it?
That simple redirects /foo/bar/ to /bar.