.htaccess rewrite multiple urls - php

I need help with this rewrite in .htaccess file.
So this what I have now ans this works but when I try to add a new RewriteRule nothing happens.
I the url that I want to be rewrite is index.php?page=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
So when I do it like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1
The page doesn't have any css when i do it like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*_)$ index.php?page=$1
The page has css but i still get index.php?page=pagetitle. But the profile page does give me /username.

RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1
Your are asking the server to redirect every URL to two different pages, it cannot work the server cannot just guess what page to load.
What you need is either a /profile/username rule or a /page/pagetitle rule.
IE something like:
RewriteRule ^profile/(.*)$ profile.php?username=$1 [QSA]
RewriteRule ^(.*)$ index.php?page=$1 [L]

Your rewrite rules are based on regular expressions and therefore need to be as specific as possible so the server can determine exactly which url to use - for example how can you tell if http://example.com/something is a page or a profile? Using a prefix such as "user", "profile", etc on your URLs means that http://example.com/profile/something can be redirected as as a username with a default redirect for everything else. To accomplish this you need to make the more specific pattern match first (users) and utilized the [L] directive to indicate that following rules should not be processed. I usually use a negative character class for URLs to match anything except a forward slash - [^/]*.
# Enable mod_rewrite
RewriteEngine On
# Set the base directory
RewriteBase /
# Don't process if this is an actual file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Does this url start with /profile and then followed with additional characters?
RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
# Assume everything else is a page
RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
Test at http://htaccess.madewithlove.be/ (note that %{REQUEST_FILENAME} and %{REQUEST_FILENAME} aren't supported for testing).
Profile
input url
http://www.example.com/profile/something
output url
http://www.example.com/profile.php
debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,QSA,L]
This rule was met, the new url is http://www.example.com/profile.php
The tests are stopped because the L in your RewriteRule options
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
Page
input url
http://www.example.com/something
output url
http://www.example.com/index.php
debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
This rule was met, the new url is http://www.example.com/index.php
The tests are stopped because the L in your RewriteRule options

Related

Multible RewriteRules result in Internal Server Error

forcing this to work makes my kind of crazy so i hope you can help.
I use Rewrite Rules and .htaccess to make my dynamic URL
example.com/page.php?id=1
look like this
example.com/1
using
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
, and it works perfectly fine so far.
But i also want to hide the filetype in the URL ( impressum.php to impressum) using
RewriteRule (.*) $1.php [L]
So both Rules are working completely correct as long as i dont use them both at the same time. When i do so, which looks like this (my complete file)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
,i get an Internal Server Error. I tried different versions, for example change the positions and so on, but i allways get this error.
So my question is: how do i get both rules together and working, while the URL ending is still hidden and the example.com/1 works too?
Thank you very much for any answer
You can use the following in your .htaccess file:
RewriteEngine on
# Check if the PHP file exists and route accordingly
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
# If not, pass the request to page.php if it contains A-Za-z0-9-
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9-]+)/?$ page.php?id=$1 [NC,L]
You need two separate rules. Rewrite conditions will only get applied to the immediately following rule and with your php extension rule, you must check that the php file exists before adding the php to the end:
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]

htaccess with url rewriting not working

This is my htaccess code:
RewriteEngine On
RewriteRule ^/typo3$ - [L] RewriteRule ^/typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
RewriteRule ^/([a-zA-Z0-9_-]+)$ http://www.example.com/index.php?id=82&user=$1 [L,R=301]
Obviously I want this URL:
www.example.com/username
to be translated to http://www.example.com/index.php?id=82&user=username
This does not work however.. (this code results in the htaccess not working at all and getting a Page not found error.
If I change the ]+$ for a ]+? the code does work, but not like I want:
RewriteEngine On
RewriteRule ^/typo3$ - [L] RewriteRule ^/typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
RewriteRule ^/([a-zA-Z0-9_-]+)? http://www.example.com/index.php?id=82&user=$1 [L,R=301]
Results in the URL being rewritten/redirected to http://www.example.com/index.php?id=82&user=index ... Exactly like that, so with user=index.
Now, if I remove the RewriteRule .* /index.php line, the htaccess again doesn't work at all anymore, resulting in a Page not found error...
I've spent days and days on figuring this out but I'm absolutely clueless..
So, I just want www.example.com/username to redirect to http://www.example.com/index.php?id=82&user=username
There are several issues here.
Inside a .htaccess file, patterns are matched "against the filesystem path, after removing the prefix". This means, you will have no leading slash as in /typo3 or ^/([a-zA-Z0-9_-]+)?
The pattern ([a-zA-Z0-9_-]+)? matches also an empty request, because of the trailing ?. I guess, this is not what you intended.
Rules are processed in sequence, unless you do a redirect [R] or add an [L] flag. This is the reason why first the request is rewritten to index.php and then in the next rule index is recognized as the user and again rewritten to .../index.php?id=82&user=index
Which leads to the next problem between the patterns .* and ([a-zA-Z0-9_-]+). .* recognizes all requests, including every user. So there is no way to distinguish between a user and any other request.
To rewrite usernames, you could try
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+) http://www.example.com/index.php?id=82&user=$1 [L]
This means, if the request doesn't correspond to an existing file !-f or directory !-d, then presume it's a username and rewrite to index.php?....
If you don't want a redirect, leave out the host name
RewriteRule ^([a-zA-Z0-9_-]+) /index.php?id=82&user=$1 [L]

htaccess for rewrite URL is not working for my website

I have dynamic URL website (irasol.com) while i navigate to menu the url shows like
http://irasol.com/index.php?id=1
I want url like this
domainname/home
domainname/aboutus
domainname/contactus
domainname/apply
home, aboutus, contactus, apply are menu name it is already in database.
my htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.php$ /index.php?id=$1 [L]
Use this instead:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
Explanation
The first three conditions make sure that domainname/aboutus is not a real file, so that we don't rewrite files that already exist.
Options -Multiviews removes a number of potential problems
In your current code, get rid of the .php in your pattern:
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
You are not matching .php extensions in the request. You are only routing matches to a query string on a real .php extension
As for a better solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L]

.htaccess - grab entire url while placing last section -([0-9]{1,5}) into second variable

Unlike most htaccess requests, where I can simply use the following code to grab the entire URL.
RewriteRule ^(.+)$ /index.php?url=$1 [L,QSA]
What I need to do, is add paging without the use of a directory ( /page/# ) in the address. So unlike /page/# in the url, the paging is just adding a dash and a number at the end of the url, such as " -3 for page three, etc.
I've tried several rewrite Rules, but I don't believe I understand apache rewriting well enough because my regular use of paging, doesn't work when applying a catchall expression.
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=something&url=$1&page=$2 [L] ## Error In? (.+)?
RewriteRule ^(.+)$ /index.php?do=something&url=$1 [L,QSA] ## Works
In the other urls, which contain direct directories such as "something", the paging will work fine.
RewriteRule ^something/([a-z]{1,6})/(.*)-([0-9]{1,5})$ /index.php?do=first&what=$1&url=$2&page=$3 [L]
What do I have to fix in the code for catchall paging. What am I missing? The address WILL have dashes, and sometimes slashes for directories.
ERROR Code:
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=this&url=$1&page=$2 [L]
Currently looks exactly like the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L]
RewriteRule ^(.+)$ /index.php?do=lists&url=$1 [L,QSA]
If I remove The line "RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L]" - everything works fine.
RewriteCond is only applicable to very next RewriteRule. Try this code:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(.+?)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L,QSA]
RewriteRule ^(.+)$ /index.php?do=lists&url=$1 [L,QSA]
AHh I see now. The RewriteCond before makes ALL the difference. A set of rewrite conditions only applies to a single redirect rule. You may need to do this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?do=lists&url=$1 [L,QSA]

htaccess second rewrite rule not working

I made a website and placed this code in the .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*)$ profile.php?id=$1 [QSA,L]
RewriteRule (.*)$ category.php?id=$1 [QSA,L]
Now, the first Rewrite rule works as expected, allowing me to have urls like site.com/profile instead of site.com/profile.php?id=foo.. but, when I added the second one, to achive the same result as the above solution, the page breaks down, and the css does not get included. So, it seems that the two can not be writen together, and I don't know what the solution is.
Try changing to:
RewriteRule ^profile/(.*)$ profile.php?id=$1 [QSA,L]
RewriteRule ^category/(.*)$ category.php?id=$1 [QSA,L]

Categories