I have successfully implemented the basic facebook like URLs:
http://example.com/(username) which internally calls http://example.com/sites/(username)
using the following mod_rewrite code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [NC,L]
Please note that the (username) folder exists inside the sites folder
The above works perfectly fine. But now i want to prettify my other urls like:
http://example.com/(username)/Image/2/ this should internally call http://example.com/sites/(username)/index.php?type=image&id=2
To do this i added the following code in the second last line:
RewriteRule ^([^/]+)/Image/(.*)$ /sites/$1/index.php?type=image&id=$2 [NC,L]
I was hoping the [L] at the end will stop the rule from being computed after this. But from the logs it turns out it goes into an infinite loop and is truncated.
Can someone point out what i'm doing wrong here?
Inside your DocumentRoot folder you can have this .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/(\d+)/?$ sites/$1/index.php?type=$2*id=$3 [QSA,L]
RewriteRule ^((?!sites[^/]+)(/.*)?$ sites/$1$2 [NC,L]
Related
I have a blog which has a number fo categories in a categories.php page. And the original URL looks like this:
http://myname.com/articles.php?category=music
Which I have simplified to:
http://myname.com/articles/music
Using:
RewriteRule ^articles/([\w-]+)/?$ articles.php?cat=$1 [NC,L,QSA]
But is there a way of simplifying this further down to:
http://myname.com/music
--
UPDATE:
I've sorted out the original problem but now I'm hitting another problem. My .htaccess is:
Options -MultiViews
DirectoryIndex articles.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ articles.php?cat=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ articles.php?cat=$1¤tpage=$2 [NC,L,QSA]
RewriteRule ^([0-9]+)/([\w-]+)/?$ article.php?id=$1&title=$2 [NC,L,QSA]
RewriteRule ^articles articles.php
RewriteRule ^categories categories.php
RewriteRule ^about about.php
RewriteRule ^feed feed.php
RewriteRule ^privacy privacy.php
When I visit http://myname.com/articles and http://myname.com/article... everything works fine, but when I go to http://myname.com/categories, http://myname.com/feed, etc I'm redirected back to the home page. Any ideas why this is?
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ articles.php?cat=$1 [L,QSA]
I know there are plenty of other questions similar to this one, i tried followinf some of those, but with no luck.
I have a website called test.com and i need, when someone seraches for test.com to show the content in test.com/home, without having home in the uerl.
My htaccess file at the moment looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.*) home/$1 [L]
RewriteRule /home/(.*) /$1 [L,R=302]
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
but when i type test.com it shows a list of folders, not the content inside home.
What am i doing wrong?
thanks
You can simplify your rules to this in root .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^$ home/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!home/).+)$ home/$1 [L,NC]
Make sure to clear your browser cache before testing this.
I have a problem with hide URL in .htaccess. I want to remove .php from the URL.
For Example: convert www.example.com/destination_info.php/Laos-destination-trip to www.example.com/destination_info/Laos-destination-trip
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/(.+)$ $1.php/$2 [L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/(.+)$ $1.php/$2 [L]
To hide .php use following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To hide html
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Try this, To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A reference to the full article can be found here
If you want to use the rule in the middle of the URL for an API like in my case, then use the first answer from #anubhava. That worked for me.
Also, I'm using IIS so I had to install the Rewrite Module and then Import the rule from the first answer. Second and Third answers will work only for the last part of the URL.
I have tried so many things in the past hour and nothing works.
I have a website called example.com
I have this code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ index.php?p=$1&i=$2 [N]
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1
When I go on example/forums/discussion-f3.html it works.
When I try /forums/introductions-f2.html, it redirects me to /index.php/?p=forums&i=introductions-f2. Also, http://craftshaft.org/forums/ sends me to http://craftshaft.org/index.php/?p=forums but when I do it without the slash and remove it from the code, it works, but I need the slash at the end so it looks like a folder.
Basically, I'd like to be able to view URLs like this:
/forums.html (to /index.php?p=forums)
forums/thread-name.html (to
index.php?p=forums&i=thread-name)
Place this code in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)(\.html)/?$ index.php?p=$1 [L,NC,QSA]
RewriteRule ^([\w-]+)/([\w-]+)\.html$ index.php?p=$1&i=$2 [L,NC,QSA]
I have in my htaccess the following:
DirectoryIndex index.py
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)$ index.py?id=$1 [L]
RewriteCond %{THE_REQUEST} /index\.py [NC]
RewriteRule ^(.*?)index\.py$ /$1 [L,R=302,NC,NE]
First rule sends someone looking for
mydomain.com/<digits>
to mydomain.com/index.php?id=, second rule fixes hrefs inside index.py.
But when I add
RewriteBase /
RewriteRule ^([^0-9]+)$ index.py [L]
To make anyone whose request is not only digits go to index.py, divs in index.py just kinda disappear. What is the problem?
Probably because you're rewriting everything to index.py, including your style sheets or scripts or whatever. Try changing the rule to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^0-9]+)$ index.py [L]