I'm stucked in a little problem:
I want to make a short link to let the user view the images of the user "test.test".
My code:
RewriteRule ^(.*)\.(.*)$/images images.php?user-id=$1.$2 [QSA,L]
Example:
website.com/tester.tester/images <== This has to be the end-result
Full Code here:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^(login|settings|friends|gallery|logout|signup|administrator)/?$ $1.php [NC,L]
RewriteRule ^home/?$ profile.php?user-id=home [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^index [NC]
RewriteRule ^(.*)\.(.*)$ profile.php?user-id=$1.$2 [QSA,L]
# RewriteRule ^([A-Za-z0-9-\s]+)\.([A-Za-z0-9-\s]+)$ profile.php?user-id=$1.$2 [QSA,L] #Umlaute müssen gehen!!
Well I tried my best and hope it will work. But I'm not happy about that:
The Updated Code:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^(login|settings|friends|gallery|logout|signup|administrator)/?$ $1.php [NC,L]
RewriteRule ^home/?$ profile.php?user-id=home [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} ^(.*)\.(.*)\/images [NC]
RewriteRule ^(.*)\.(.*)\/images$ images.php?user-id=$1.$2 [QSA,L]
RewriteCond %{THE_REQUEST} ^(.*)\.(.*)\/images\/ [NC]
RewriteRule ^(.*)\.(.*)\/images\/$ images.php?user-id=$1.$2 [QSA,L]
RewriteCond %{THE_REQUEST} !^(.*)\.(.*)\/images\/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^index [NC]
RewriteCond %{THE_REQUEST} ^(.*)\.(.*) [NC]
RewriteRule ^(.*)\.(.*)$ profile.php?user-id=$1.$2 [QSA,L]
RewriteCond %{THE_REQUEST} ^(.*)\.(.*)\/ [NC]
RewriteRule ^(.*)\.(.*)\/$ profile.php?user-id=$1.$2 [QSA,L]
The Result is correct, but if the user types in "tester.tester/" wrong page is loaded.
This is annoying me. BUT I tried it and even this little thing might be solved by PHP (I don't really know)
Thanks for all Answers!! :D
You closed the expression prematurely with $, which would cause an error.
This should work for you:
RewriteRule ^(.*)\.(.*)/images$ images.php?user-id=$1.$2 [QSA,L]
Your file should now look like this:
RewriteEngine on
RewriteRule ^(login|settings|friends|gallery|logout|signup|administrator)/?$ $1.php [NC,L]
RewriteRule ^home/?$ profile.php?user-id=home [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^index [NC]
RewriteRule ^(.*)\.(.*)\/images$ images.php?user-id=$1.$2 [QSA,L]
RewriteRule ^(.*)\.(.*)$ profile.php?user-id=$1.$2 [QSA,L]
# RewriteRule ^([A-Za-z0-9-\s]+)\.([A-Za-z0-9-\s]+)$ profile.php?user-id=$1.$2 [QSA,L] #Umlaute müssen gehen!!
Related
I want to redirect all below pattern url to before "?" url
i.e
Xyz.com/worksheet/rebus/?random=testing11
Xyz.com/worksheet/rebus/?abc
Xyz.com/worksheet/rebus/?l=1
should be redirected to
Xyz.com/worksheet/rebus/
I tried many things but not able to succeed. How can i do using .htaccess
Rules tried
RewriteBase /worksheet/
RewriteRule ^rebus/?$ /worksheet/rebus/ [L,R=301]
Overall
RewriteEngine On
RewriteBase /worksheet/
RewriteCond %{THE_REQUEST} \s/(rebus)/\?(\S+)\s [NC]
RewriteRule ^ /worksheet/%1/ [R=301,L]
#RewriteRule ^rebus/?$ /worksheet/rebus [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
Have it this way:
RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus? [L,R=301,NC]
# skip all rules for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
How do I rewrite this?
somepage.php?parameter1=value1¶meter2=value2¶meter3=value3 to
somepage/parameter1/value1/parameter2/value2/parameter3/value3
and also redirect to that for older indexed URLs.
Also, I want it to work for only two of those parameters, or one, or none.
I've tried this, but doesn't seem to work that well:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteRule ^([^/]*)/parameter1/([^/]*)$ $1.php?lang=$2 [PT]
You can use this generic rule to rewrite /somepage/n1/v1/n2/v2 to /somepage.php?n2=v2&n1=v1
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteRule ^(somepage)/([^/]*)/([^/]*)(/.*)?$ /$1/$4?$2=$3 [L,QSA,NC]
RewriteRule ^(somepage)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
You will have to create a rule for each possible situation, with 2 parameters, 1 parameter and no parameters.
RewriteEngine On
RewriteRule ^([^/]*)/parameter1/([^/]*)/parameter2/([^/]*)(/?)$ $1.php?
parameter1=$2¶meter2=$3 [NC]
RewriteRule ^([^/]*)/parameter1/([^/]*)(/?)$ $1.php?parameter1=$2 [NC]
RewriteRule ^([^/]*)(/?)$ $1.php [NC]
The (/?) at the end is to also catch the situations where the link ends with a possible slash.
I am trying to create friendly pagination using htaccess file.
But its not working I guess I am using wrong rule for files.
Check out my codes below.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /search/%1/? [R=301,L]
RewriteRule ^search/$ search/%1 [L,R=301,NC]
RewriteRule ^search/(.*)/$ search.php?q=$1 [L,NC]
RewriteRule ^new/(.*)$ new.php?page=$1 [L,NC]
RewriteRule ^(.*)/$ cat.php?id=$1 [NC]
RewriteRule ^(.*)/(.*)/$ cat2.php?id=$1&page=$2 [NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id=$1 [NC]
Everything is working fine, only cat2.php is not working.
How to fix it?
You need to switch the order around between your 2 cat rules. (.*) matches everything, including slashes, so it will always match whatever cat2 matches. Try just swapping their orders and include L flags:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /search/%1/? [R=301,L]
RewriteRule ^search/$ search/%1 [L,R=301,NC]
RewriteRule ^search/(.*)/$ search.php?q=$1 [L,NC]
RewriteRule ^new/(.*)$ new.php?page=$1 [L,NC]
RewriteRule ^(.*)/(.*)/$ cat2.php?id=$1&page=$2 [L,NC,QSA]
RewriteRule ^(.*)/$ cat.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id=$1 [NC]
I have this .htaccess file:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profil.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profil.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/galerie$ galerie.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/galerie/$ galerie.php?user=$1 [NC]
RewriteRule ^(.*)/galerie-(.*)_(.*)$ galerie_poze.php?user=$1&album=$2&page=$3 [NC]
RewriteRule ^(.*)/galerie-(.*)_(.*)/$ galerie_poze.php?user=$1&album=$2&page=$3 [NC]
RewriteRule ^(.*).php?$ index.php?request=$1 [QSA,L]
and it doesnt seem to work.
Here is my problem: when I want to access a page on my site e.g. site.com/test.php,
it redirects to index.php?request=test. The problem is that I also have "profil.php" and "galerie.php" that I dont want to pass through index.php. Instead when I do this: site.com/username/profil, I want it to point to profil.php?user=username, but it doesnt work.
Any suggestions?
i've put something like this:
RewriteCond %{REQUEST_URI} !^(.*)\.php?$
RewriteRule ^(.*)\.php?$ index.php?request=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ profil.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profil.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/personal$ personal.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/personal/$ personal.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/galerie$ galerie.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/galerie/$ galerie.php?user=$1 [NC]
RewriteRule ^(.*)/galerie-(.*)_(.*)$ galerie_poze.php?user=$1&album=$2&page=$3 [NC]
RewriteRule ^(.*)/galerie-(.*)_(.*)/$ galerie_poze.php?user=$1&album=$2&page=$3 [NC]
and when i do /username or /username/profil it works BUT it no longer works with the first rule id i do sitename.tld/test.php it doesnt work....
You want one of your two first rules to match [username]/profil (where it now matches [username] and [username]/)
Try
RewriteRule ^([a-zA-Z0-9_-]+)/profil$ profil.php?user=$1 [NC, L]
Use conditionals.
Make sure the file or directory doesn't exist:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
You can also restrict redirects by using something like:
RewriteCond %{REQUEST_URI} !^/profil\.php$ [NC]
Make sure to properly escape.
This needs to be escaped:
RewriteRule ^(.*).php?$ index.php?request=$1 [QSA,L]
Like:
RewriteRule ^(.*)\.php?$ index.php?request=$1 [QSA,L]
Also,
RewriteRule ^([a-zA-Z0-9_-]+)$ profil.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profil.php?user=$1 [NC]
Can be simplified to:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profil.php?user=$1 [NC]
I have a site I've written in php.
The addresses are as follows:
http://www.site.com/page.php
I'd like any request to:
www.site.com/page.php
or
www.site.com/page
To go to:
www.site.com/page/
(And force a www.)
Can anyone help me?
For domain :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
For your first directory :
RewriteRule ^/([^/]*)(\.php)? /$1/
RewriteRule ^page.php$ http://www.site.com/page/ [QSA,L]
RewriteRule ^page$ http://www.site.com/page/ [QSA,L]
Maybe this will work.
This should do what you wanted:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
Tim's answer is the closest, but it also does adds the trailing slash to images and css...
So taking Tim's answer, and slightly editing it gives us:
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
Which should work!