Access the entire URL path in RewriteRule - apache

Currently, my RewriteRule only works when I use the current path, for example loading the URL
http://localhost/rest/v1/test
Redirects to the URL:
http://localhost/rest/v1/index.php?url=test
Great. However, I want to be a bit cleverer about this and redirect to the correct version of the API so I don’t have these hard coded values. However my RewriteRule only works when I include the current path component. Here is my rule:
RewriteRule ^(test)$ https://localhost/rest/v1/index.php?url=$1 [QSA,NC,L,R]
How can I achieve getting the version path component and passing it in to the redirect URL?

I figured it out. I was testing in a htaccess file with intent of adding it to the config file. I didn't realise (although it's obvious now I know) that htaccess only has the scope of its own folder.

Related

htaccess add index.php to the route with more paths after it

I am building a REST using framework.
Normally I can call my web services by going to this route
https://www.domainname.com/webservices/index.php/something/else/here
Now I want to be able to hide index.php but still call the same route like this
https://www.domainname.com/webservices/something/else/here
Currently I am able to get 200-OK from only going up to webservices/
using the following htaccess rewrite:
RewriteRule ^webservices/(.*)/$ webservices/index.php/$1 [R,L]
however, everything after the webservices/ will give me a 404 not found
here is an example.
webservices/ ---200-OK (I see the page of webservices/index.php
webservices/something ---404 not found
webservices/some/thing ---404 not found
I guess the biggest question is how do I add the paths after I add the index.php (the level of paths should be dynamic)
P.S. I don't want to display index.php in the URL at all

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 :)

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.