I have a problem with rewriting URLs in my website. I managed to remove all .php extensions from my URLs and addd rewriting all mydomain.com/img?id=X to mydomain.com/img/X, but I have been struggling with one more thing for some time and I would appreciate any help. My website consists of many pages and the link to page number X is mydomain.com/?page=X (e.g mydomain/?page=3), but I want to rewrite that URLs to mydomain.com/page/X
Here is my .htaccess:
RewriteEngine on
RewriteBase /
<nothing important>
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^img/([0-9]+)/?$ img.php?id=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This rule will rewrite /?page=X to /page/X
RewriteRule ^page/([0-9]+)/?$ /?page=$1
Related
I am using the following htaccess code. However this code means that my images or CSS are not loading, on the plus side it means that .php or the trailing / are needed like my client has asked for.
How can I make this work so that the images and the CSS and JS etc load correctly?
I would like the url to look like www.example.com/services or www.example.com/services/
currently google shows up both
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
# enforce a trailing-slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
# remove php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
See also
Htaccess: add/remove trailing slash from URL
Remove .php extension with .htaccess
I have a problem with rewriting hrefs in my website. First of all I managed to remove all .php extensions from na URLs, then I added rewriting all mydomain.com/img?id=X to mydomain.com/img/X, but I have been struggling with one more thing for some time and I would appreciate any help. My website consists of many pages and the link to page number X is mydomain.com/?page=X (e.g mydomain/?page=3), but I want to rewrite that URLs to mydomain.com/page/X
Here is my .htaccess:
RewriteEngine on
RewriteBase /
<something not important>
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^img/([0-9]+)/?$ img.php?id=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Right below this rule:
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]
try adding:
RewriteCond %{THE_REQUEST} \ /+\?page=([0-9]+)
RewriteRule ^ /page/%1? [L,R]
RewriteRule ^page/([0-9]+)/?$ /?page=$1 [L]
Im trying to access two of my pages and rewrite their urls with .htaccess.
First page I'm trying to access:
http://api.aotikbot.tv/polls.php?username=aotik
Second page I'm trying to access:
http://api.aotikbot.tv/getpoll.php?poll=917529
My current .htaccess code:
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ polls.php?username=$1 [NC,L]
RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ getpoll.php?poll=$1 [NC,L]
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html
# End of Apache Rewrite Rules
</IfModule>
The first page works as /polls/username but the second doesn't read the poll value and I think redirects to polls.php.
Could anyone explain what I'm doing wrong?
Thanks in advance
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^[A-Za-z0-9-]+/([0-9-]+)/?$ getpoll.php?poll=$1 [NC,L]
RewriteRule ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ polls.php?username=$1 [NC,L]
Here, I've swapped your two rules around, so we check for IDs first (for getpoll - note how I have removed letters from the expression). If we're not doing an ID check (ie, there are letters in the segment), then it'll rewrite for polls.
Update:
Per arco444, if the username is a number, this wouldn't work. This is a more specific solution:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^getpoll/([0-9-]+)/?$ getpoll.php?poll=$1 [NC,L]
RewriteRule ^polls/([A-Za-z0-9-]+)/?$ polls.php?username=$1 [NC,L]
I have to completely replace an existing website. The current site is completely spagetti code with some rewrite rules to mimic friendly urls.
There are some mission critical issues that can't be resolved with the current architecture and database structure, so for a time both code bases need to live side by side.
This is the current .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ $1.php?q=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3&s=$4
I'm replacing it with a zend framework site, but it routes everything through the index.php file for the routing
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule .* index.php
Does anyone have any idea how I can combine the two until the original can be completely replaced?
If you can map your old URLs to Zend Url someway, then it would be rather easy. Let's say you had
http://foo.bar.com/something.php?q=arg1&r=arg2
then if you have this functionality working in Zend code, then you must also have valid URL there, like
http://foo.bar.com/something/q/arg1/r/arg2
If so, all you need is to rewrite that old URLs to Zend URL and instead of doing internal redirection, do HTTP redirection, i.e. (out of my head, not tested):
RewriteCond %{QUERY_STRING} ^q=(.*)&r=(.*)^
RewriteRule ^something\.php$ /something/q/%1/r/%2? [R=301,L]
Mind the trailing "?" which tells mod_rewrite NOT to attach original query string to rewritten one (otherwise you would end with /something/q/%1/r/%2?q=X&r=y. Note we do regular 301 HTTP redirection here.
They're not going to merge very easily, it's a matter of which one you want to have precedence. You could add a few more conditions to make sure everything doesn't get routed as a $1.php file, then add the zend rules to the end.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} ^([^/]+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/([^/]+)/$ $1.php?q=$2
RewriteCond %{REQUEST_URI} ^([^/]+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3
RewriteCond %{REQUEST_URI} ^([^/]+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ $1.php?q=$2&r=$3&s=$4
# other rules
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule .* index.php
I have created a php web site. Here I have implemented the clean URL in .htaccess file. I need to display mu link
mydomain/index.php to mydomain/index
So I have used
RewriteRule index index.php
But I have lot of files. So how will I write a common rule for this entire page?
E:g
RewriteRule hotels hotels.php
Thanks
try this:
#Setup
Options +FollowSymLinks
RewriteEngine On
#Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
#Your thing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]