Htaccess to rewrite url for an existing php app - apache

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.

Related

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]
# ...

Apache mod_rewrite .htaccess and conditions to avoid rewriting while url mysite.com

I'd like to make custom URL for my users so they can have their personnal art gallery displayed just like on facebook when it displays user's profile . For instance mysite.com/username will display username's gallery.
So basicaly what I've done now is something like that :
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?fc=module&module=testmodule&controller=displaygallery&url=$1 [L]
I have the gallery correctly displayed on my website, but I can't display the index page anymore using absolute mysite.com. I know we can use conditions to avoid such a situation but which one ?
So while writing the post I've just think about the line I've done and I found the solution ^^ !
Instead of using :
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?fc=module&module=testmodule&controller=displaygallery&url=$1 [L]
I'm now using :
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?fc=module&module=testmodule&controller=displaygallery&url=$1 [L]
Instead of using * I used +, which is telling Apache that it needs at least one character or more to rewrite URL so everything works fine now :)

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

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.

Prevent users from accessing files using non apache-rewritten urls

May be a noob question but I'm just starting playing around with apache and have not found a precise answer yet.
I am setting up a web app using url-rewriting massively, to show nice urls like [mywebsite.com/product/x] instead of [mywebsite.com/app/controllers/product.php?id=x].
However, I can still access the required page by typing the url [mywebsite.com/app/controllers/product.php?id=x]. I'd like to make it not possible, ie. redirect people to an error page if they do so, and allow them to access this page with the "rewritten" syntax only.
What would be the easiest way to do that? And do you think it is a necessary measure to secure an app?
In your PHP file, examine the $_SERVER['REQUEST_URI'] and ensure it is being accessed the way you want it to be.
There is no reason why this should be a security issue.
RewriteCond %{REDIRECT_URL} ! ^/app/controllers/product.php$
RewriteRule ^app/controllers/product.php$ /product/x [R,L]
RewriteRule ^product/(.*)$ /app/controllers/product.php?id=$1 [L]
The first rule will redirect any request to /app/controllers/product.php with no REDIRECT_URL variable set to the clean url. The Rewrite (last rule) will set this variable when calling the real page and won't be redirected.

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.