RewriteRule will not work in .htaccess file - php

I have found several questions on Stack that have show lots and lot of examples, but I have tried everyone in the book and this refuses to work.
I am trying to convert.
www.domain.com/title/index.php?id=70048305
to
www.domain.com/title/70048305
I want it to be converted both ways. When they type in with the query string and with the clean url.
What is wrong with my rewrite?
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^title/(.+)$ index.php?id=$1 [L,QSA]
I should note that I've placed this .htaccess file both on the root and in the /title/ folder.

Try this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-+/]+)$ index.php?id=$1
RewriteRule ^([a-zA-Z0-9-+/]+)/$ index.php?id=$1

Don't match ^title/(.+)$ you need to be matching only numbers like so ^title/([\d]+)/?
Otherwise,
www.domain.com/title/index.php?id=70048305
will match and be redirected to:
index.php?id=index.php?id=70048305

Related

How to Make .htaccess search by allow mod_rewrite as query string and page number?

My original file is a PHP page named search.php.
Normally, the search function will be:
mydomain.com/search.php cid=&type=search&q=keyword+text&page=2
However, is it possible to rewrite it to mydomain.com/all.html?q=keyword+text&page=[1-anypage]?
I tried to put this code into .htaccess:
RewriteRule ^all.html?q=([^.*]+)&p=([0-9]+)$ search.php?cid=&type=search&q=$1&p=$2 [L]
but something went wrong.
Please help me find the solution. Thanks.
Yes its possible but you can't match against query string in pattern of a RewriteRule, you will need to use RewriteCond directive to check %{QUERY_STRING} something like the following
RewriteEngine On
RewriteCond %{QUERY_STRING} ^q=([^&]+)&page=(.+)$ [NC]
RewriteRule ^all\.html$ /search.php?cid=&type=search&q=%1page=%2 [L]

PHP clean URL with no php extension

This is my .htaccess code.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ movie.php?name=$1
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.in$
RewriteRule ^/?$ "http\:\/\/example\.in" [R=301,L]
ErrorDocument 404 /404.php
AddDefaultCharset UTF-8
Header unset ETag
FileETag None
I need a clean URL for my website.
I've consulted lots of tutorials and forums and created the above code.
But it's not working. Almost I'm fighting with code.
I don't understand the clean URL concept clearly. Are there any codings I have to write in my PHP file?
<a href='movie.php?name=titanic'> Titanic </a>
I have this link in my index.php file.
I'd like example.in/movie/titanic when I click on the link "Titanic".
Also I'd like to get the value by $_[request].
What exactly do I have to do? Please don't mark this question as duplicate, I've searched a lot and haven't got the concept clearly. Please help me out.
Thanks.
The rules in .htaccess work on url's relative to the directory your .htaccess file is in. If you have a .htaccess in your www-root, then the first argument of RewriteRule will match everything behind the domain name and before the query string (movie/titanic in http://example.com/movie/titanic?is=amovie). To fix the rule, you need to change the rule to:
RewriteRule ^movie/([a-z0-9]+)/?$ movie.php?name=$1 [L,NC]
The [L] flag stops rewriting for this round, the [NC] flag ignores case.
Besides that, you only need one RewriteEngine on, which needs to be above all rules you write. You can safely delete the second one.
You can put your .htaccess into main Directory:
RewriteEngine On
RewriteRule ^movie/([a-zA-Z0-9]+)(/)$ movie.php?name=$1 [L]
Or in movies/ directory:
RewriteEngine On /movies/
RewriteRule ^([a-zA-Z0-9]+)(/)$ ../movie.php?name=$1 [L]
While movie.php is in main Directory located.
RewriteEngine On can by starterd only once per htaccess.

Use .htaccess to hide .php and query name

I have been searching all over google and stackoverflow, and have been finding a large number of answers but none seem to work for me.
Basically what I want is to rewrite my url localhost/index.php?page=1 to localhost/1, while still keeping the query alive.
Now, I have managed to remove the .php with the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But removing the Query simply does not seem to work.
Please help me out.
Your rule doesn't match what you're trying to do. You're telling it to take 1 or more of anything and make it into blah.php (like http://localhost/blah -> http://localhost/blah.php).
You're looking for something like RewriteRule ^([0-9]+)$ index.php?page=$1 [NC,L].
By the way, your language is a bit backwards. You're not rewriting index.php?page=1 to localhost/1; you're rewriting localhost/1 to index.php?page=1 (well, really 1 to index.php?page=1).

Apache mod_rewrite and regular expression

In my HTML I want to refer to my images like this:
/images/articles/this-is-the-page-title-dgj4klci5j4.jpg
/images/articles/thumbs/this-is-the-page-title-dgj4klci5j4.jpg
/images/gallery/this-is-the-page-title-qj3k56l6kfm.jpg
but they are really located at:
/_pics/articles/dgj4klci5j4.jpg
/_pics/articles/thumbs/dgj4klci5j4.jpg
/_pics/gallery/qj3k56l6kfm.jpg
My .htaccess file is currently in the root directory of the site and looks something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website\.com [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [R=permanent,L]
RewriteRule ^articles/ user/articles.php [L]
RewriteRule ^gallery/ info/gallery.php [L]
I have to do this because in the last possible moment the client decided that he wants more legible image filenames and this seems like a quick reasonable fix.
I imagine that I could redirect everything from /images to /_pics and take the page title out by just using mod_rewrite. Problem is that I'm not very good with regular expressions and I don't fully understand the way mod_rewrite works. Can I get some help? Thanks
The real image filename only contains letters and numbers and can be jpg or png.
Try adding this rule in the htaccess file:
RewriteRule ^images/(.+)/[^/]+-([^/-]+)\.(jpe?g|gif|png)$ /_pics/$1/$2.$3 [L]

Apaches mod_rewrite issues

I'm having issues with apaches mod_rewrite. I'm wanting to make clean urls with my php application but it doesn't seem to give the results i'm expecting.
I'm using this code in my .htaccess file:
RewriteEngine on
RewriteRule ^project/([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
RewriteRule ^project/([0-9]{4})$ /project/index.php?q=$1 [L]
To make it so when I view, http://localhost/user/project/system, it would be the equivelant of viewing http://localhost/user/project/index.php?q=system
Instead of getting any results I just get a typical 404 error.
I've also just checked to see if mod_rewrite works by replace my .htaccess code with this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) http://www.stackoverflow.com
And it properly redirects me here, so mod_rewrite is definitely working.
The root path to my project is /home/user/public_html/project
The the url used to view my project is http://localhost/user/project
If anymore information is required let me know.
Thanks
If your .htaccess file is indeed located in the project/ subdirectory already, then don't mention it in the RewriteRule again. Remove it:
RewriteRule ^([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
# no "project/" here
Rules always pertain to the current local filename mapping.
Else experiment with a RewriteBase.
You have [0-9]{4} in your regex which will only match numbers of 4 digits. "system", however, is not a number of 4 digits, and therefore does not match.
You can use something like [^/]+ instead.
RewriteRule ([^/]+)/([0-9]{2})$ /index.php?q=$1&r=$2 [L]
RewriteRule ([^/]+)$ /index.php?q=$1 [L]
Don't know if the second parameter should be a number with 2 digits or not.
Edit: I also added "user" at the beginning now.
Edit2: Okay, I thought you were in the root htdocs with your htaccess. So remove "project" and "user" if you are in "project" with the .htaccess.
You probably mean
RewriteRule ^/project/([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
RewriteRule ^/project/([0-9]{4})$ /project/index.php?q=$1 [L]
The '^project' means "start of line is 'project'" but the start is a '/project', so you need to include the starting slash (i.e. '^/project...').
Sorry, missed the system bit (and the user bit). Was concentrating on the slash.
RewriteRule ^/user/project/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)$ /user/project/index.php?q=$1&r=$2 [L]
RewriteRule ^/user/project/([a-zA-Z0-9]*)$ /user/project/index.php?q=$1 [L]
Should have you right.

Categories