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

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]

Related

how to change url to a clean url that works

I've been looking for an answer for my problem, however the things I tried didnt work out. What I've been trying to do is to create a beatiful url for this link:
mywebsite.com/blog_template?slug_url=blog-post-name
to
mywebsite.com/blog-post-name
To achieve this I tried the following code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog_template/([^/]+)$ /blog_template?slug_url=$1 [L]
But my code didnt work... Any advice?
Here’s the full htaccess my website:
RewriteEngine On
RewriteBase /
ErrorDocument 404 http://91.218.67.117/404/
ErrorDocument 500 http://91.218.67.117/500/
#redirect 404
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
#remove php
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
Considering you want to rewrite this request in backend to index.php(OR change it to appropriate file's name in case its some other php file). With your shown samples and attempts please try following .htaccess Rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/blog_template\?slug_url=(\S+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?slug_url=$1 [QSA,L]
ErrorDocument 500 /pages/errors/500.php
NOTE: Please keep your .htaccess file and index.php(OR any php file which is taking rewrite request in backend for that matter) in same path(your root etc).
Your attempt was close. Just a small fix:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /blog_template?slug_url=$1 [L]
This relies on additional rewriting rules to get applied though, since /blog_template most likely is not a resource the http server can somehow "execute" immediately. So you may want to combine above rule with other rules, which you did not reveal to us, which is why I cannot say anything about that.
If you also want to redirect requests to the "old" URL, then that variant should do:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^slug_url=([^/]+)$
RewriteRule ^/?blog_template$ / [QSD,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /blog_template?slug_url=$1 [L]
It is a good idea to start out using a R=302 _temporary_redirection and to only change that to a R=301 permanent redirection once everything works as expected. That prevents nasty caching issues on the client side.

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]

Using more than one friendly url with Mod_Rewrite

I was able to get the urls to work using this code
RewriteRule ^(.*)/([^/\.]+)/?$ $1/index.shtml?dynContent=$2 [L]
So dynContent = apples or bananas or bananas-apples
So if I type in http://example.com/dir1/apples or /dir1/bananas or /dir1/bananas-apples it displays using the correct content I am pulling into index.shtml
However I have one page that needs to be full page with and I have one page configured to go full page and it is named as indexFP.shtml
The dynContent=oranges for this one page
I have the RewriteRule as
RewriteRule ^(.*)/([^/\.]+)/?$ $1/indexFP.shtml?dynContent=$2 [L]
But this does not call up the correct page with full page for this one page.....
When I type in http://example.com/dir1/oranges it uses index.shtml instead of indexFP.shtml
Is there a way to do this? Here is what I have now.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/\.]+)/?$ $1/index.shtml?dynContent=$2 [L]
RewriteRule ^(.*)/([^/\.]+)/?$ $1/indexFP.shtml?dynContent=$2 [L]
Any suggestions?
[L] means "last" in mod_rewrite. That stops processing so your indexFP rule above will never get hit.
What I think you are trying to do is choose index for some strings and indexFP for others. To do that you need a way to choose one rule over the other. My rewrite-fu is a little rusty but something like:
# match "oranges"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /oranges/
RewriteRule ^(.*)/([^/\.]+)/?$ $1/indexFP.shtml?dynContent=$2 [L]
# match everything else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/\.]+)/?$ $1/index.shtml?dynContent=$2 [L]

How to remove and avoid url directories in htaccess

I'm trying to allow my site to rewrite urls. I have put the following into my .htaccess file in the root directory.
RewriteEngine On
#would be nice to remove member-pages from the URL but no idea how.
#RewriteRule ^members/(.*)/?$ /$1 [NC,R]
#This part works though!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ ./members/$1/ [L]
So far, it takes
mydomain.com/someUserName or mydomain.com/someUserName/ (with trailing slash) and, if it exists, will load the page at mydomain.com/members/someUserName/ without a hitch. This works like a gem.
What I want now (and am trying to do with the first rewrite rule) is to take a mydomain.com/members/someUserName or mydomain.com/members/someUserName/ and have it show up as mydomain.com/someUserName in the url.
How do I do this? Thanks in advance!
If I understand you correctly, You want to redirect domain.com/members/foo to domain.com/foo , You can use the following rule for that:
RewriteEngine On
RewriteCond %{THE_REQUEST} /memebers/([^\s]+) [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ./members/$1 [NC,L]

mod_rewrite Rules Giving 404

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]