Apache ignore htaccess - apache

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]

Related

Apache Server redirection issue on .htaccess

I have written HTML page and want to set RewriteRule. I have created .htaccess file into the folder written these:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ test.html [QSA,L]
It just redirects to http://example.com/test.html . I want to redirection to http://localhost/project/test
I think it is caused by httpd.conf or hosts file. But I can't find a way.
Edit: .htaccess file is under C:\xampp\htdocs\project
Thank you.
You may use this rule with appropriate RewriteBase value:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ test.html [L]
I dont understand why do you want to use .htaccess file for redirect. Also note You can not move your site to localhost. You may directly do from your file
String site="/abc.com/test.html"
Response.setStatus(response.SC_MOVED_TEMPORARILY)
Response.setHeader("Location", site);

how to rewrite URL using .htaccess file?

I am having problem in rewriting a url using .htaccess file. I am not aware of regex. I have red so many blogs and used online rewrite rule generator to generate url and pasted in .htaccess file. But no one is worked. I have enabled mod_rewrite module in httpd.conf file in apache server.
I want to rewrite a below url.
I have developed project in OS - Win7 with Apache server.
http://localhost:81/quora/tamilnanban/innerpage.php?url=why-you-should-avoid-watching-porn-on-android-smartphones
to
http://localhost:81/quora/tamilnanban/why-you-should-avoid-watching-porn-on-android-smartphones
my current .htaccess file contains below codes:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
#RewriteRule ^url/([^/]*)\.html$ :81/quora/tamilnanban/innerpage.php?url=$1 [L]
RewriteRule ^/quora/tamilnanban/([^/]*)\.html$ /quora/tamilnanban/innerpage.php?url=$1 [L]
This above rule also not working. Kindly help me to achieve url rewriting using .htaccess file. Also if i am doing any mistakes in the above steps, please correct me. Thanks in advance.
To convert /quora/tamilnanban/innerpage\.php\?url=foobar" to /quora/tamilnanban/foobar you can use the following rules in /quora/tamilnanban/.htaccess :
RewriteEngine on
#1)Redirect "/quora/tamilnanban/innerpage\.php\?url=foobar" to "/quora/tamilnanban/foobar"#
RewriteCond %{THE_REQUEST} /quora/tamilnanban/innerpage\.php\?url=(.+)\sHTTP [NC]
RewriteRule ^ /quora/tamilnanban/%1? [L,R]
#2)The rule bellow will internally map "/quora/tamilnanban/foobar"" to "/quora/tamilnanban/innerpage\.php?url=foobar"#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ /quora/tamilnanban/innerpage\.php?url=$1 [L]

Rewriting ends up with not loading js / css

With the following .htaccess file, I obviously, get alot of 404's on loading scripts and css because the browser keeps looking in the wrong directory.
I am a newb at htaccess and have no clue about how to fix it.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^$ client/app/index\.html [L]
</IfModule>
Dir structure is as follows:
project /client / app / index.html
.htaccess is located in project directory.
I recomend you that redirect users with a 301 to correct path. This will end with your problems.
RewriteRule ^$ client/app/index\.html [L, 301]
If you are worried about seo look at https://support.google.com/webmasters/answer/93633?hl=en
All of the calls at your server are being redirected to your RewriteRule. Add a RewriteCond (RewriteCondition) to ignore CSS and JS files, if they exist:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^$ client/app/index\.html [L]
Explanation:
RewriteCond %{REQUEST_FILENAME} !-f
If the requested filename exists (css/main.css), do not go through with the RewriteRules.
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
If the file does not exist, but matches one of the extensions, do nothing. This is helpful because you get a 404 if css/main-2.css does not exist, rather than redirecting you to index.php.
And then finally is your own rule, which redirects all non-conditioned rules.
In your .htaccess file, this is what worked for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^ /index.html [R=301]
Thank you!

.htaccess RewriteRule not working, bad flag delimiters

My site previously uses URL's like this: /folder/page
Previously, you could prepend 'panel' in the URL to edit the current page: /panel/folder/page
We upgraded our CMS, and the new URL to edit the page is in this format: /panel/#/pages/show/folder/page
I am trying to add a rewrite rule so that we can still use the old way, but can't get it to work:
RewriteCond %{REQUEST_URI} !^/panel/#/
RewriteRule /panel(.*) /panel/#/pages/show/$1
Is there a way to do this? A 301 redirect should work too, I think.
Edit: here is my existing .htaccess:
RewriteEngine on
RewriteBase /
# make panel links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^panel/(.*) panel/index.php [L]
# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
Since "panel" is already used, I ended up using this redirect rule:
RedirectMatch 301 /admin(.*) /panel/#/pages/show/$1

URL rewrite doesn't rewrite page

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