To enable the url rewrite with Apache - 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.

Related

Apache mod_rewrite not working even trying different solutions:

I have been trying to shorten URLs with apache .htaccess file, and currently I have this code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*)$ show.php?page=$1 [NC, QSA, L]
AllowOverride All
With this code, for example https://example.com/welcome should be redirected to https://example.com/show.php?page=welcome, but it is not. When I run sudo a2enmod rewrite, I get the following:
Module rewrite already enabled
I also tried to run service apache2 restart. How can I solve it?

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]

Rewrite url page not found .htaccess

I'm trying to rewrite my url with .htaccess to beautify my urls but I'm getting page error. What surprises me is that it's working on my friend's machine using xampp and I'm using apache on my linux machine.
# Turn Rewrite Engine On
RewriteEngine On
# Rewrite for info.php?id=xx
RewriteRule ^photos/([0-9]+) info.php?id=$1 [NC,L]
# Rewrite for profile.php?user=xx
RewriteRule ^user/([0-9a-zA-Z_-]+) profile.php?user=$1 [NC,L]
# Rewrite for logout.php?redirec=xx
RewriteRule ^logout/([0-9a-zA-Z]+) logout.php?redirect=$1 [NC,L]
The most probable cause of error appears to be that mod_rewrite is not loaded. Check apache's httpd.conf file, and search for the line:
LoadModule rewrite_module modules/mod_rewrite.so
If there is a # before it, remove that and restart the server.

mod-rewrite RewriteRule ignored in document root .htaccess file

I have the following at the top of the .htaccess file in the root of my virtual host:
RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/blog(/.*)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/mobile(/.*)?$ [NC]
RewriteRule (.*) /? [R=301,L]
</IfModule>
This block validates out using the htaccess tester and returns the desired results. However, when I hit the webserver -- Apache 2.4.6 -- I only receive 404 errors when requesting http://www.domain.com/blog; with or without the trailing slash. The conf file for the vhost allows overrides and is configured to allow the following of symlinks. I also verified that httpd is seeing the .htaccess file by adding some junk in the file which resulted in a http status of 500; the desired results.
The reason that I am not using Redirect or RedirectMatch is that I need to quell the querystring from the initial request. Otherwise, either of those directives works fine. Any ideas as to why this is failing on the server and not the tester is greatly appreciated. Thanks.

Apache rewrite_module

I have a problem trying redirect some user requests to my Apache server pointing to non existing files, using something like this:
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^/a/b/c.* /d/e$1 [L]
My intention is that, if some user requests file http://whatever.com/a/b/c/something, which doesn't exist, instead of sending the 404 error, rewrite the URL to get it from http://whatever.com/d/e/something, where I can be sure the file do exist.
I know there are a lot of topics on this, but I did very intense research, and my problem is no matter what combination I try, I can't seem to get the rewrite module to work..
Even with something like this :
RewriteRule ^(.*) /index.html [L]
That would presumably redirect EVERY petition to index.html , but it does nothing.
It is supposed to be loaded, as apache2ctl -M outputs rewrite_module as correctly loaded
Logging errors at debug level, I only see, users requesting non-existent files, and Apache answering '404 File does not exist' errors..
I'm the server admin so I'm not using .hcaccess, I edit directly the httpd.conf file, so there should be no overriding directives issues.
What am I missing?
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
...to redirect user to the index page.