mod_rewrite Rules Giving 404 - apache

I'm sure this has been answered, I've been reading for a few hours now and am getting nowhere. I need this to work tonight and my brain is hurting, so I'm asking the the wonderful internet for help.
I'm trying to run all my pages through index_new.php (it all made sense when I decided to do that, I swear). I have two types of pages, static and dynamic. The dynamic pages are all the same kind of page, but with different data based on the database (MySQL). I'm trying to make these rewrites
/about => index_new.php?page=about
/installations => index_new.php?page=about
/installations/{site_id} => index_new.php?page=site&siteID={site_id}
(I'd love if about could be generic, but I don't want to push my luck) My .htaccess file is:
# enable rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# rewrite all physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/statics/" [OR]
RewriteCond ${REQUEST_URI} "/ajax/"
# rewrite rules
RewriteRule ^about index_new.php?page=about
RewriteRule ^/installations index_new.php?page=list
RewriteRule ^/installations/(.*) index_new.php?page=site&id=$1
When I try to go to /about or any other page, I get HTTP 404. As a note, my root is http://localhost/~user/public_html/new and the .htaccess file is there.

Provided:
The conditions in the question have to be met for each rule. (They are repeated as they are valid only for the next rewrite rule).
The .htaccess file is located at root.
You may try this instead of the rule-set in your question:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /about/?$ [NC]
RewriteRule .* /index_new.php?page=about [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /installations/?$ [NC]
RewriteRule .* /index_new.php?page=list [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /installations/([^/]+)/? [NC]
RewriteRule .* /index_new.php?page=site&id=%1 [NC,L]
Maps silently:
http://example.com/about to
http://example.com/index_new.php?page=about
http://example.com/installations to
http://example.com/index_new.php?page=about
http://example.com/installations/Anything to
http://example.com/index_new.php?page=site&Anything
For permanent and visible redirection, replace [NC,L] with [R=301,NC,L].
NOTE:
These conditions in the question were not used as their purpose is not clear:
RewriteCond %{REQUEST_URI} "/statics/" [OR]
RewriteCond ${REQUEST_URI} "/ajax/"
To include them for one or several rules, try this:
# For NOT condition, include the next line before the corresponding rewrite rule:
RewriteCond %{REQUEST_URI} !(statics|ajax) [NC]
# For positive condition, include the next line before the corresponding rewrite rule:
RewriteCond %{REQUEST_URI} (statics|ajax) [NC]

Related

URL Rewrite - Multiple query

I am trying to see how I can achieve the following rewrite rules.
From
https://localhost/site/?page=place&place=west
https://localhost/site/?page=location&location=cityname
To
https://localhost/site/place/west
https://localhost/site/location/city
I am able to change
https://localhost/site/?page=place to https://localhost/site/place but not with another additional query as mentioned above.
htaccess
RewriteEngine On
#Redirect /site/?page=foobar to /site/foobar
RewriteCond %{THE_REQUEST} /site/(?:index\.php)?\?page=(.+)\sHTTP [NC]
RewriteRule ^ /site/%1? [L,R]
# Internally rewrite new path to the original one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:site/)?(.+)/?$ /site/?page=$1 [L,QSA]
The above htaccess works for the following which is what I also need.
https://localhost/site/place
https://localhost/site/about
https://localhost/site/contact
With your shown attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs. New rules are clubbed to your already existing rules.
RewriteEngine ON
RewriteBase /site/
##New rules from here......
RewriteCond %{THE_REQUEST} \s/site/?\?page=([^&]*)&place=([^&]*)\s [NC]
RewriteRule ^ /site/%1/%2? [R=301,L]
# Internally rewrite new path to the original one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^site/([^/]*)/(.*)/?$ index.php?page=$1&location=$2 [NC,QSA,L]

friendly url only opens the post.php not cat.php

Friends, what is the right way to get both the php files to open friendly URL contents?
My current code works OK if I only use:
Options +FollowSymLinks
RewriteEngine On
# SEO URL Settings
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ post.php?id=$1 [QSA,L]
but then I also need friendly URL for my categories so I tried to add:
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ cat.php?cat=$1 [QSA,L]
but doing so only the post URL opens but category links redirect back to index.php but if you remove the rewrite for post than the cat.php contents shows.
If someone could help me out here would really appreciate your kindness.
This is because of your use of the [L] flag.
The [L] flag causes mod_rewrite to stop processing the rule set. In
most contexts, this means that if the rule matches, no further rules
will be processed. This corresponds to the last command in Perl, or
the break command in C. Use this flag to indicate that the current
rule should be applied immediately without considering further rules.
Documentation
Instead, try and have your rules laid out like this:
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ post.php?id=$1 [QSA]
RewriteRule ^(.*)$ cat.php?cat=$1 [QSA,L]

how to fix htaccess error with multiple rewrite rule - ERR_TOO_MANY_REDIRECTS

why this htaccess has error. it says "to many redirect" please help thanks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://us.domain.com/ae_en/$1 [R=301,L]
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteRule ^(.*)/ae_en/%{REQUEST_URI} [R=301,L]
I cannot try the wrong code, it always appear error
There are several issues with your .htaccess. I would suggest both re-ordering and editing the rules.
This would be a better approach, in my opinion. I'll explain the parts below.
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
# External redirect: Remove www from domain
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# External redirect: Ensure language code is set
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^(.*)$ /ae_en/$1 [R=301,L]
# Internal redirect: Pass everything non-existent to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
Structure
First, general options and settings should come at the top and only once, i.e.
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
Second, I like to get the external URL to show up correctly, so I group all external redirects here - denoted with comments in the above code block.
Third, internal "redirects" follow, meaning mapping the (now correct and final) external URL to the correct scripts.
This structure ensures that the internal scripts are only matched on a URL that will not change when re-run against the .htaccess on the next RewriteRule match.
Omitting external redirect for index.php
I guess you added the index.php redirection handling after adding in the internal redirect, which probably has caused index.php to show up in the browser. So, with the above structure in place, I assume it will not show up again.
Error in last RewriteRule
As Mike Rockett pointed out in the comment above, there also was an error in the last RewriteRule.
Avoid redirect loop
To avoid a redirect loop when rewriting to index.php with the last rule, you have to explicitly exclude index.php from language code redirection, i.e.
RewriteCond %{REQUEST_URI} !^/index.php [NC]

htaccess remove get parameter keys not value

I am trying to remove the get keys from my url,
So test?category=innate&index=0 would become test/innate/0.
I have been trying all day to no avail. I find resources very hard to learn apache. Any point in the right direction would be most grateful.
Add these rules to the htaccess file in your document root
RewriteEngine On
# to externally redirect
RewriteCond %{THE_REQUEST} \ /test\?category=([^&]+)&index=([0-9]+)
RewriteRule ^test$ /test/%1/%2 [L,R=301]
# to internally rewrite back
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([^/]+)/([0-9]+)$ /test?category=$1&index=$2 [L]
This rule should work for you:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^test/([^/]+)/([^/]+)/?$ test?category=$1&index=$2 [L,QSA,NC]
Reference: Apache mod_rewrite Introduction

Use specific PHP file for specific directories

I am trying to create mod_rewrite rules that rewrite based on the first level directory name plus a failover to rewrite to a standard file in case none of the directory names are matched.
Example:
I have units.php, models.php and other.php. The other.php file should handle all non-assigned requests.
http://www.mydomain.com/units/4435
Should redirect to /units.php?id=4435
http://www.mydomain.com/models/594
Should redirect to /models.php?id=594
http://www.mydomain.com/anything
Should redirect to /other.php?id=anything
http://www.mydomain.com/anything/893
Should redirect to /other.php?id=anything/893
Let me know if this makes sense. I am unsure of how to structure the rules and conditions to achieve what I want.
This is what I have tried to sfar. It works for URLs starting with 'units' or 'models' but I get a 500 Error if I try any other URL:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(units)/(.+)$ /units.php?id=$2 [L,NC]
RewriteRule ^(models)/(.+)$ /models.php?id=$2 [L,NC]
RewriteRule ^(.+)$ /other.php?id=$2 [L,NC]
I would suggest this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(units|models)/([0-9]+)/?$ /$1.php?id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(units|models)/[0-9]+/?$
RewriteRule ^/[^/]+/([0-9]+)/?$ /other.php?id=$1 [L,QSA]