What.htaccess code should I write to turn something like
localhost/article.php?id=something&number=something
into just
localhost/article/id
Thoughts guys?
You can use rules like this in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+article\.php\?id=([^\s&]+) [NC]
RewriteRule ^ article/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^article/([^/]+)/?$ article.php?id=$1 [L,QSA,NC]
Related
I have not enough experience in writing rules in .htaccess.
My site url is like
http://exampletest.com/detail.php?i=my-url
I want to rewrite that url to
http://exampletest.com/my-url
What I have tried so far:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} /detail\.php\?i=([^\s&][A-Za-z0-9-]+) [NC]
RewriteRule ^ detail %1? [R=302,L]
That makes me able to access my site using:
http://exampletest.com/detail/my-url
But I want to omit detail from url as well.
You should redirect internally as well so your code should looks like this :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} /detail\.php\?i=([^\s&][A-Za-z0-9-]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /detail.php?i=$1 [L]
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} /detail\.php\?i=([^\s&][A-Za-z0-9-]+) [NC]
RewriteRule ^ %1? [R=302,L]
I have a URL like:
http://localhost/admin-s/index.php?m=user
However, I would like a URL like this:
http://localhost/admin-s/user
I try like this:
RewriteRule ^([a-z0-9]+).php$ index.php?nol&m=$1 [L]
RewriteCond %{QUERY_STRING} ^m=([a-zA-Z0-9]+)$
RewriteRule ^index.php$ user%1.php [L]
How to make it?
Hope this simple configuration will help you out. You can check this configuration here
This will redirect request of this pattern from http://localhost/admin-s/index.php?m=user to http://localhost/admin-s/user
Explanation:
1. If REQUEST_URI is /admin-s/index.php
2. If QUERY_STRING is m=someWords
3. Redirect to /admin-s/someWords
.htaccess
RewriteEngine on
Options -MultiViews
RewriteCond %{REQUEST_URI} \/admin\-s\/index\.php
RewriteCond %{QUERY_STRING} m=([\w]+)
RewriteRule ^(.*)$ /admin-s/%1? [R=301,L]
The question was answered here : How to turn dynamic URL into static URL
For your problem may be try following rules:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^admin-s/([^-]+) /index.php?m=$1 [R=301,QSA,L]
Let us know if it works
You can use the following rule in your root/.htaccess or /admin-s/.htaccess :
RewriteEngine on
#redirect /admin-s/index.php?m=user to /admin-s/user
RewriteCond %{THE_REQUEST} /admin-s/index\.php\?m=([^\s&]+) [NC]
RewriteRule ^ /admin-s/%1? [L,R]
#rewrite the new uri back to the orignal location
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:admin-s/)?(.+)$ /admin-s/index.php?m=$1 [NC,L]
Try below in your admin-s direcotry,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)$ index.php?m=$1 [L]
I'd like code for a .htaccess file that when this is entered:
domain.com/event/granniesteaparty a file in the folder 'event' (display.php) takes the 'granniesteaparty' bit as though domain.com/event/display.php?search=granniesteaparty had been entered.
If tried all sorts of things but this is where I am at the moment:
RewriteEngine on
RewriteRule ^event/([A-Za-z0-9]+)/([0-9]+)/?$ /event/display.php?search=$1 [NC,L]
RewriteCond %{QUERY_STRING} s=([^&]+)
RewriteRule ^event /event/1? [NC,R=301]
I hope that makes sense, could someone point me in the right direction please?
You have too many capture groups in your first rule. Give this a try
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} ^GET\ /+event/display\.php\?search=([^&\s]+)
RewriteRule ^ /event/%1? [R=301,L]
RewriteRule ^event/(\w+)/?$ /event/display.php?search=$1 [NC,L]
Put the following code .htaccess inside event directory:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !\s/+event/display.php?search= [NC]
RewriteRule ^(.*)$ event/display.php?search=$1 [R=302,L,NE]
RewriteRule ^event/display.php?search=$1(.*)$ /$1 [L,NC]
Update
If you want only to redirect any wrong page to the display page but internally so the following code will do it :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) event/display.php [L,NC]
So when someone request /event/whateverwrong it will be at the url as it is but internally will map to display.php
Ok i've tried multiple versions of code on my .htaccess file and I still can't get it to work properly... I don't know if I'm putting it in the wrong folder or what is going on but, here is what I'm trying to accomplish.
This is my code
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([a-z0-9]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
Here is another version that I've tried
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
Here is what I'm trying to accomplish... a URL that looks like this:
example.com/rentals/32746/florida
The "32746" and the "florida" come from a form on the index page of the site and then it gets passed over to the "rentals" folder through the URL.
The .htaccess code sorta works whereas it spits out a URL like this:
http://www.example.com/rentals/12345/arkansas?zipcode=12345&location=arkansas
But do you see the extra on the tail end? It's as if it's duplicating the results in the URL.
I currently have the .htaccess file in my "rentals" folder should it be in the root folder?
UPDATED VERSION
For those who were running into the same problem as myself... here is the actual code I ended up going with:
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
You need to add a ? to the end of the target path in your first Rule :
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
As it descards the orignal query strings from the destination.
Clear your browser's cache before testing this.
I have the php file page.php.
URLS:
page.php?content=contact
page.php?content=aboutus
I need it to be shown like this:
mysite.com/contact
mysite.com/aboutus
I figured out that I have to make .htaccess
RewriteEngine on
RewriteRule ^paginas/([^/]+)/?$ page.php?page=contact [L]
Tryed many things.. No results..
Thanks for attention
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(?:GET|HEAD)\ /+page\.php\?content=([^&\ ]+)
RewriteRule ^ /%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /page.php?content=$1 [L]
This will first redirect: /page.php?content=anything to /anything then internally rewrite back requests like /anything to /page.php?content=anything.