Apache htaccess: rewrite directory to query param - apache

I know there are quite some questions out there about this issue but I have not yet been able to find the exact way how to do a rewrite in my case:
Having an application available at https://example.com/dev/myapp/#/?query=1234, I would like to allow an alternative access using this URL: https://example.com/dev/myapp/1234 without creating a sub-directory /1234.
Using the htaccess, I would like that the latter URL is rewritten/ forwarded to the former URL.
What I have tried:
RewriteEngine On
RewriteRule ^myapp/([^/])?$ myapp/index.html?query=$1 [L]
will forward
https://example.com/dev/myapp/1234
to
https://example.com/dev/myapp/1234#/
And
RewriteEngine On
RewriteRule ^(?!myapp/)([^/])?$ /dev/myapp?query=$1 [L,R=302]
will forward
https://example.com/dev/myapp/1234
to
https://example.com/dev/myapp/?query=1234#/
I am somehow stuck with getting the syntax right. Any help?

If I good understood, I create regex with group contain the numbers and re-used it for your query parameter, with NE flag for "noescape" output and keep "#" syntax, the doc.
A. You can use in your virtualhost, the code below, the url will changed.
RewriteEngine On
RewriteRule ^/dev/myapp/([0-9]+)$ /dev/myapp/\#/?query=$1 [L,R=301,NE]
B. If you need to keep url of source, enable in httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
And you can use in your virtualhost, the code below. (P for proxy)
SSLProxyEngine on
RewriteEngine On
RewriteRule ^/dev/myapp/([0-9]+)$ /dev/myapp/\#/?query=$1 [P]
I hope that will help you

Related

httpd redirect with rewrite and condition not working as expected

I'm having trouble with my Apache redirects and rewrites, and I have no idea why what I've got doesn't work. This is the goal:
Browser URL: http://127.0.0.1/app-gallery/all
Redirect (301) to: http://127.0.0.1/apps/all
Serve file from: /en/apps/all
In my httpd.conf, I have the following:
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RedirectMatch 301 ^/app-gallery(.*) /apps$1
RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^(.*)$ /en/$1 [L,QSA]
With the last two lines (condition and rule) included, my browser does not get a 301 redirect, just a 404. If I take the last two lines out, the redirect happens - but then I'm obviously not serving a file from the /en/ directory like I need to be.
Shouldn't the redirect happen, resulting in a new request that doesn't get redirected, followed by application of the rewrite?
Also, I have a number more of these RedirectMatch lines in there (after the relevant one), but I removed them for sake of simplifying my example.
For simple redirections always go for redirect, but if you need to use mod_rewrite, better stick with it, that is, mixing redirect and rewrites is going to cause confusion, like this actual case.
What is happening is Rewrite is happening first, and both your rewrite directives and redirectmatch apply over the same thing (both origin are not /end/, defining redirect first does not make it happen first.
What you want is to describe something that will go in the order you want:
RewriteRule ^/app-gallery(.*) /apps$1 [R=301]
RewriteRule ^/(?!en)(.*) /en/$1 [L,QSA]
This redirects in the order of preference you want.
If the final destination is in the filesystem, you could try to use Redirect and Alias with I believe something like this, but I am not sure about this since I don't know your setup well enough:
Redirect permanent /app-gallery/ /apps/
AliasMatch ^/apps/(.*) /filesystem/path/to/apps/$1

Apache mod_rewrite showing no effect

I am using WAMP, the latest version with Apache 2.4.9. I was trying to beautify my URLs using mod_rewrite, that's preinstalled in my system. I uncommented the # before this line in httpd.conf:
#LoadModule rewrite_module modules/mod_rewrite.so
became
LoadModule rewrite_module modules/mod_rewrite.so
restarted the server, and wrote the following re-write rule in my .htaccess:
RewriteEngine on
RewriteRule ^localhost/testmod/(\W+).php$ localhost/testmod/$1
I saved it in a directory named testmode. Without modifications, URL of a file named test.php inside this folder looks like: localhost/testmode/test.php and I want to strip out the .php from the URL.
It doesn't change anything. The URL is same, localhost/testmode/test.php.
Am I making any mistake in the regexp or something else is wrong?
You have to turn that around:
RewriteEngine on
RewriteRule ^testmod/(\W+)$ testmod/$1.php [L]
Think of it like that: the incoming request is to http://localhost/testmod/test, that string has to be matched by the regex pattern. Then you internally map that the the physical script http://localhost/testmod/test.php in your file system.
Also make sure that the interpretation of such .htaccess style files is enabled in your http server.
And two general notes:
Since it is your system I assume you have control over the configuration of the http server. If so it always is better to place such rewriting rules (and similar stuff) in there instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and really slow the server down. They are only meant as a last fallback for those who do not have access to the configuration of the http server.
Note that you need a slight modification of the rule syntax if you decide to place the rules in the real server configuration:
RewriteRule ^/testmod/(\W+)$ /testmod/$1.php [L]
Your question is answered in the documentation of the apache module you want to use. You really should read that documentation. It is precise, complete and offers really good examples: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you have .htaccess inside testmod folder then these rules should work for you:
RewriteEngine On
RewriteBase /testmod/
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

MASKING URL with HTACCESS

I am trying to redirect visitors of alldomain.com to the domain newdomain.com it does redirect however I want that when the user open's alldomain.com the data should be of newdomain.com however the top url should be alldomain.com
My Current HTACCESS:
RedirectMatch .* http://www.newdomain.com
I believe the solution to this consist of two parts: correct .htaccess, and using mod_proxy on your Apache server:
Uncomment these lines in httpd.conf (and restart Apache!):
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Add the following lines to your .htaccess:
RewriteEngine on
RewriteRule .* http://www.newdomain.com/ [P]
ProxyPassReverse / http://www.newdomain.com/
The key here is that the [P] flag in the RewriteRule tells Apache to use mod_proxy (which you enabled earlier), and the ProxyPassReverse makes sure that any links from the new domain are properly "attributed" to the old domain as well. I think that does it, but I can't test... Let me know whether this works for you!

To enable the url rewrite with Apache

Want im trying to acheive,
when url is hitting localhost/nap/ , internally it should call the url localhost/nap/inspect.do
To acheive this, Using Apache 2.2, following are the steps i followed,
Uncomment the line, LoadModule rewrite_module modules/mod_rewrite.so
Added he following line in httpd.cnf file
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/nap/
RewriteRule ^(.*)$ /nap/inspect.do$1 [L]
restarted the apache server
But this configuration doesnt help me
You need only this in httpd.cnf file:
RewriteEngine On
RewriteRule ^/nap/$ /nap/inspect.do [L]
All requests with /localhost/nap/ will be redirected to run the file $DocumentRoot/nap/inspect.do script. L flag ignores all additional rewrite rules in the same file.

mod_rewrite does not rewrite

I have the following code:
RewriteEngine on
RewriteRule ^user/([a-z]+)$ index.php?Agent=$1 [QSA]
theoretically any visitor to my site using the format:
www.site.com/user/username
should be directed to:
www.site.com/index.php?Agent=username
they are instead being treated as if /user is a real directory and as if /user/username is a valid file and exists (which it does not). Any ideas?
EDIT #1
I have modified my htaccess slightly and even stripped off the /user requirement, the following .htaccess file causes and 500 error, even when visiting the primary domain alone (www.site.com):
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.+)$ index.php?Agent=$1 [QSA]
they are instead being treated as if /user is a real directory and as if /user/username is a valid file and exists (which it does not).
What exactly do you mean like this? What does it show?
Do you want to rewrite or **redirect?
Rewrite = call www.site.com/index.php?Agent=username when the user enters www.site.com/user/username
Redirect = redirect the user to the other URL so their address field says www.site.com/index.php?Agent=username
Right now, your code should rewrite, not redirect, which is probably what you want. Just checking...
Your problem might be that the username you're testing with contains capital letters. Add NC to make the match case insensitive.
RewriteRule ^user/([a-z]+)$ index.php?Agent=$1 [QSA,NC]
If you want to allow other characters than just the letters a-z, you can either add more characters to character class, or just write (.+) to match any character.
Edit
As per the question below, I think the infinite redirect happens because the rule is triggered again (and again) because the rewrites matches the new index.php request. Simply checking that the target of the redirect isn't a file should solve that. (-f = "is file". ! = negate condition.)
RewriteCond %{REQUEST_FILENAME} !-f
It should work. I am using pretty much that exact line for my rewrite. Here's what I've got:
RewriteRule ^history/(.+)?$ history.php?display_name=$1 [QSA,L]
Note the "L" flag, which means mod_rewrite should stop at this point, and not process any other rewrite rules. Do you have any other rewrite rules after this one?
Are you putting this in .htaccess? Make sure it's being processed at all. Put some random junk in there and load a page. Apache should show you an error page, complaining about a server misconfiguration. If it doesn't, it's not even looking at your .htaccess file. Make sure in your apache configuration, "AllowOverride" is set to "FileInfo Options" for the directory in which this webpage lives.
This is a stab in the dark but I suggest that you recheck your Apache configuration file apache2.conf file to make sure that mod_rewrite is enabled.
Look for a line like this :
#LoadModule rewrite_module modules/mod_rewrite.so
and change it to:
LoadModule rewrite_module modules/mod_rewrite.so
EDIT: Then restart your web server for the changes to take effect!
Hope this helps!