URL rewrite doesn't rewrite page - apache

I tried to use th URL Rewrite module, but it won't rewrite the URL for some reason. If I go to this website: http://localhost/projectredrum/foto.php it will show the correct page, but it doesn't rewrite the URL to what I want.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /projectredrum/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^foto\.php$ /Aquaria-Foto/Fotos
</IfModule>
Rewrite Module Tutorial
After looking at the tutorial mentioned above I figured I should try without
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Because if you use this, it will only rewrite the URL when the URL doesn't match a file name or directory.
However when I did that I got a 404 page not found issue.
Does anyone know why the URL rewrite doesn't work?

Apache does local rewrite, because page is in same server it can load it on same request. Add [R,L] to end of RewriteRule line to rediret browser to wanted address.
R means temporally Redirect,
L means last rule.
R=301 is permanent redirect
eg [R=301,L] does permanent redirect and stops checking other rules.
In your case, you probably want to use this kind line:
RewriteRule ^foto\.php$ /Aquaria-Foto/Fotos [R,L]
Here is more info about flags: http://httpd.apache.org/docs/2.4/rewrite/flags.html

It is not working because of this condition:
RewriteCond %{REQUEST_FILENAME} !-f
since http://localhost/projectredrum/foto.php is a valid file.
To fix the rule you can use this in /projectredrum/.htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /projectredrum/
RewriteCond %{THE_REQUEST} /foto\.php[?\s] [NC]
RewriteRule ^ /Aquaria-Foto/Fotos [L,NC,R=302]
R=302 is used to actually redirect the URL in browser.
In DocumentRoot/.htaccess you can use:
RewriteEngine On
RewriteRule ^Aquaria-Foto/Fotos/?$ /projectredrum/foto.php [L,NC]
References:
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Apache mod_rewrite In-Depth Details

Related

.htaccess URL rewrite not working, tried all possible options

I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example

override apache rewrite rules with htaccess

working on a server that hosts multiple domains.
Somewhere in apache config is a rewrite rule that is set for ALL domains.
What happens is if a user goes to example.com/foo they are supposed to get redirected to example.com/foo_bar
However, in one domain, I want to override this behavior so that the url stays at example.com/foo and does not redirect. I've been searching and trying various rules and conditions to no avail. I don't have access to the apache config, but I am using .htaccess for some rewrite rules on this domain.
here's my rewrite rules in .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
# this isn't working
# RewriteRule ^(foo)($|/) - [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1
# this didn't work either
# RewriteRule ^/foo index.php?/$1 [L]
</IfModule>
Try redirecting /foo_bar back to /foo.
When Apache processes the rewrite rules it runs through them multiple times, which you can see when you turn on debugging. I think what's happening in your case is that you're trying to match the URI in the first pass, but Apache has already modified it to /foo_bar.
Also, as a matter of debugging, you should try to recreate the problem in an environment you control. Ask your sysadmin for a copy of the global configuration and mirror the set up you're constrained to.
You can create exception for one domain:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteRule ^foo(/.*)?$ /foo_bar$1 [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

How to redirect URLs based on query string?

I successfully mass migrated a Wordpress site to Drupal. Unfortunately in Wordpress, the content URL's were something like www.example.org/?p=123. My domain is still the same, but I want to do a redirect via htaccess as Drupal will not allow URL's to be www.example.org/?p=123. In other words, the content does not have the same URL as it did in Wordpress. For example, the new Drupal URL would be something like www.example.org/content/MyNewPage
I tried this in my .htaccess file and it does not work
Redirect 301 /\?p=375 http://www.example.org/content/MyNewPage
So I tried the below, but it does not work either.
Redirect 301 /\?p\=375 http://www.example.org/content/MyNewPage
Just as a test, I tried the below and it worked.
Redirect 301 http://www.example.org http://www.google.com
I made sure that my Redirect rule is at the top of the list in my .htaccess so it will be evaluated first. How do I fix this?
neither Redirect nor RedirectMatch allow you to specify a query string for the redirect source.
[Source]
You have to use mod-rewrite for redirecting based on query string:
RewriteCond %{QUERY_STRING} ^p=375$
RewriteRule (.*) http://www.example.org/content/MyNewPage? [R=301,L]
You may consider use ModRewrite in your htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^p=345$ [NC]
RewriteRule index.php content/MyNewPage [NC,L,R=301]
</IfModule>
And you also may want to pass the old page id to the new URL concatenated (or maybe by QS?):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule index.php content/MyNewPage-%1 [NC,L,R=301]
</IfModule>

Apache ignore htaccess

I have a website that uses mod_rewrite for pretty urls. I have two main urls:
example.com/id
example.com/generate/id
And this htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^generate/([a-z0-9_-]+)$ generate.php?id=$1 [L,NC]
RewriteRule ^([a-z0-9_-]+)$ contact.php?id=$1 [L,NC]
The first url is working correctly, but the second not. Apache shows a 404 error, I think that it's because it's looking for the folder "generate", that doesn't exist, and it can't find the htaccess in the document root.
Thank you
Have you try
RewriteRule ^generate\/([a-z0-9_-]+)$ generate.php?id=$1 [L,NC]

Mod_rewrite forward everything to index.php 's params

How can I get anything but files to rewrite to index.php's params? I'm using apache, mod_rewrite, etc.
If you want to rewrite anything that isn't a request for a regular file I'd use this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/index\.php -f
RewriteRule ^(.*)$ /index.php?page=$1 [QSA]
</IfModule>
This way you don't need to concern yourself about adhering to a certain URL format for your rewrites - you can test and act accordingly in your serving script.
Try this:
RewriteRule /([^/]*)/([^/]*)/([^/]*)[^\.].* /index.php?var1=$1&var2=$2&var3=$3 [R=301]
This will redirect url like /foo/bar/baz to /index.php?var1=foo&var2=bar&var3=baz