Hi there I'm trying to make a blog demo and I'm having some pretty URL codes already.
i am having a url www.xyz.com
and a search url www.xyz.com/search/this+is+a+search+text
in search url the parameter search is a page name and this+is+a+search+text is a parameter that i'll be parsing
I'm having a .htaccess code below already
# code to make pretty URLS | we're using this code to achieve /category/slug
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\d]+)$ app/index.php?page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)$ app/post.php?category=$2 [L]
I'm using the below code for serach page
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php&searchstring=$2&page=$3 [L,QSA]
But while using the code I get a 500 internal error message, I'm not able to figure out what's the error!
I would really appreciate if anyone could help me out with this logic.
Rewrites in achieve /category/slug and for search page are the same. Search request matched with first - achieve rewrite and run app/post.php...
To answer your followup question to JarekBaran
JarekBaran: Rewrites in achieve /category/slug and for search page are the same.
What this means is that the RewriteCond are the same for the pretty URL code for the caterogy/slug page and the search page.
This means that the first match will always be used and so:
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
will always be triggered before
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
This way you will not be able to use the search function. You could consider adding a parameter to identify if a search is made or not.
Your problem is that you are starting your rules with (.+) which will match one or more of anything. This means that you are also matching the / character. Some of your 2 parameter redirects are matching your urls with 3 parameters because of this. You would be better of starting with something like this - ([a-zA-Z0-9-]+)
Your search rewrite should probably be something like this - RewriteRule ^/?([a-zA-Z0-9-]+)/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
I tested that rewrite on this htaccess testing tool and it is working.
That said your other rules will need to be changed as well since they all start with (.+). After doing that your category rewrite will conflict with your search rewrite.
However, this would probably work for you:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?search/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9-]+)/([\d]+)$ app/index.php?page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)$ app/post.php?category=$2 [L]
Related
Here's what I have so far...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
So site.com/page works to view site.com/page.php.
What I want to add to this is on one particular page where there is a query string...
example: site.com/song.php?name=Song-Name -> I want it to be site.com/song/Song-Name.
I know there are a lot of post similar to this and I've looked through a ton but just don't understand quite how to accomplish this.
You will need a new rule to handle /song/ URIs:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^song/(.+)$ song.php?name=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ $1.php [L]
I have also corrected your 2nd rule since query string is already appended to target.
original = http://ritetag.techreanimate.com/signin.php?network=twitter&fhfghdfghh=sadfgsdf
rewrite = http://ritetag.techreanimate.com/signin/twitter?fhfghdfghh=sadfgsdf
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^signin/([^/]*)$ /signin.php?network=$1
i got this working but i cant get other variables using get that come after, how do i get toe rewrite to work for stuff other than network.
I solved it!
RewriteEngine On
RewriteBase /
#This rewrite has no L flag so the second one can continue
#Signin.php can have multiple networks like signin/twitter = signin.php?network=twitter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^signin/([^/]*)$ /signin.php?network=$1 [QSA]
#this rewrite just allows to use files withouth the .php
# Like about instead of about.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [QSA]
I've just started using mod_rewrite, this is what I use for a quite basic structured website with multiple language support:
RewriteEngine on
ErrorDocument 404 /error404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})/([^/]+)$ $2.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})/$ index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})$ index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ $1.php [L]
My idea was that languages are indicated at the beginning of the URl and need to be two characters (a-z or A-Z). After that there will be a string that refers to a php-file which has the same filename as the string, just .php attached. The language will be added as a GET-Variable (?lang=en).
So, an URl could look like this: /en/index and should then be redirected to index.php?lang=en.
If the URl does not start with a language, but directly with the string, then no GET-variable will be attached to the string. E.g. /index should refer to index.php. If there is no string, then index should be used as default.
So far, so good. Works fine. Just if I enter a string (no matter if I use 2 characters for language or not), the site always shows an 500 Internal Server Error, instead of going to error404.php. Also, if I delete this line (404) in .htaccess, it is still the same error.
So, I assume that there is something wrong with the other lines in the .htaccess that cause this error, does anybody have an idea what this could be?
Any help is highly appreciated!
Your rules are looping. At a glance, it looks like your last one:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ $1.php [L]
Say I have this URL, http://your.domain.com/blah, and the file blah.php doesn't exist. This is what's happening.
URI = blah, !-f is true, it's not a file
URI = blah, !-d is true, it's not a directory
URI = blah, internal rewrite to blah.php
blah != blah.php, rewrite rules loop
URI = blah.php !-f is true, it's not a file
URI = blah.php !-d is true, it's not a directory
URI = blah.php, internal rewrite to blah.php.php
blah.php != blah.php.php, rewrite rules loop
This goes on until the rewrite engine has had enough and returns a 500 server error.
You can do one of 2 things here:
Add a directive to make all looping stop no matter what, which is an easy to get around this kind of stuff. But it will break rules that require looping. I don't see anything like that in your rules so it's safe (at least for now) to do this. Just add this to the top of your rules:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Do a pre-emptive check to see if the php file actually exists:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)$ $1.php [L]
Helmut, try this (the last rule is the reason for the 500 internal error, and this also includes the suggestion of #DaveRandom to merge rule 2 and 3):
RewriteEngine on
ErrorDocument 404 /error404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})/([^/]+)$ $2.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})/?$ index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)$ $1.php [L]
I am doing a project with fetching data from database. I am used URL rewriting method. I was using rewrite method it will redirect to error page
Dynamic url
http://www.sample.com/?cat=kk
The rewritten URL
http:/www.sample.com/kk
the .htaccess file written for this URL rewrite
RewriteEngine On
RewriteRule ^([^/]*)$ /?cat=$1 [L]
The .htaccess file also contain another url rewrite
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&year=$3&page=$4 [L]
RewriteRule ^([^/]*)/([^/]*)$ /?cat=$1&sub=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$ /?cat=$1&sub=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /?cat=$1&sub=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&pag=$3 [L]
RewriteRule ^([^/]*)/$ /?cat=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
The problem I am having is that when I access any page, it will redirect to server default error page.
I'm sorry to say that, but your rewriterules are messy.
Always start from the most complex to the simplest one.
And you've forgotten one of the most important directives: QSA. I let you google for this ;)
Maybe what follows still doesn't work, but it's cleaner.
We need log files to know what's happening.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&year=$3&page=$4 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&page=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /?cat=$1&sub=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /?cat=$1 [QSA,L]
I've been using this htaccess code to pass on vars to redirect.php which handles the includes for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)/(.*)$ redirect.php?value1=$1&value2=$2&value3=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)$ redirect.php?value1=$1&value2=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ redirect.php?value1=$1 [L,QSA]
but i noticed that it will not go to redirect if all arguments is empty, so if i go to http://domain.com/ it will open index.php, but if i go to http://domain.com/any-param/ redirect.php handles it correctlty. How can I make it always use redirect.php as default, even when no additional URL parameters is set?
Your rules appear to be fine to me. Just add this line in the end:
RewriteRule ^$ redirect.php [L]
This will redirect http://domain.com/ to http://domain.com/redirect.php.
use to do it like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z]+)?/?([a-z]+)?/?([-a-z0-9]+)?/?$ [NC]
RewriteRule ^([a-z]+)/?([a-z]+)?/?([-a-z0-9]+)?/?$ /redirect.php?value1=$1&value2=$2&value3=$3 [NC,L]
Note: Even with only 1 param, this will still work as it just leaves the other 2 blank. So you don't have to copy and paste thew same code while removing just one condition.
I found that this is not very scalable. Now I just redirect everything to the index.php and let that file fetch and dissect the URI to handle my system layout. This allows for a very scalable system.