url construction htaccess - .htaccess

Lets say I have a link to one of my page that looks like: mysite.com/48YSWD96, I need it to look like: mysite.com/?d=48YSWD96. How do I achieve this? Can I achieve this by modifying my htaccess file? which currently looks like this...
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -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]
RewriteBase /

It looks like you just need to append this to the end:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9]+)$ /?id=$1 [L]
For it to work the other way around:
RewriteCond %{QUERY_STRING} (^|&)id=([A-Za-z0-9]+)($|&)
RewriteRule ^$ /%1 [L]
This will take a request for mysite.com/?d=48YSWD96 and change the URI to /48YSWD96. Essentially, whatever the id equals in the query string.

Related

.htaccess call PHP file with parameters

Sorry if the title of the question is not clear, please read below for details;
I have this .htaccess:
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/home/.*$ index.php [QSA]
One of its functions is to redirect the url from this example:
www.example.com/user.php?user=david
to be:
www.example.com/david
Now, I have a file friends.php which function is to show friends of a particular user.
So the link that I wanted is like this:
www.example.com/david/friends
How can I achieve this?
You have to basically do the same as you did for user. In fact, the rule does not even conflict, because the user rule cannot contain a / character.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/friends$ friends.php?user=$1 [L]

.htaccess single variable url rewrite

I am trying to redirect http://test.pearlsquirrel.com/explore_all?u=hiphop to http://test.pearlsquirrel.com/explore_all/hiphop using htaccess. However, It keeps redirecting here: http://test.pearlsquirrel.com/explore_all/?u=site_error.php. I have looked all over Stackoverflow for the answer but I just can't seem to find it. I would really appreciate some help in fixing this problem, thanks!
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) explore_all?u=$1 [L]
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} [^=]+=([^=]+)
RewriteRule .* /%1? [L]
Will map this:
http://test.pearlsquirrel.com/explore_all?u=hiphop
To this:
http://test.pearlsquirrel.com/explore_all/hiphop
Replace the corresponding code in your .htaccess file with this one.
I have not checked the rules in your .htaccess file.
OPTION
Now, if it is the other way around, which I think it is, and the idea is to map this incoming URL:
http://test.pearlsquirrel.com/explore_all/hiphop
To this resource:
http://test.pearlsquirrel.com/explore_all?u=hiphop
Replace the above rule set with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/?
RewriteRule .* http://test.pearlsquirrel.com/explore_all?u=%1 [L]

.htaccess - Querystring without index.php

I want my url's to look like this: "http://www.example.com/start",
and not like this: "http://www.example.com/index.php/start".
That works fine with this code in my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1
On one page i have to work with GET-Parameters, the url should look like this:
"http://www.example.com/artists/picasso",
and not like this:
"http://www.example.com/index.php/artists?artist=picasso"
That's also possible with the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /index.php?/$1
RewriteRule ^artists/(.*)$ /index.php/artists?artist=$1 [L]
Now, the problem with this solution is, that all other url's now have to end with a slash.
So it has to look like this: "http://www.example.com/start/",
and not like this: "http://www.example.com/start"
Thanks for your help !
Try changing your rules to:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !artists/
RewriteRule ^(.*?)/?$ /index.php?/$1
RewriteRule ^artists/(.*)$ /index.php/artists?artist=$1 [L]
Mainly, you have a /$ at the end of your pattern: RewriteRule ^(.*)/$ /index.php?/$1 which means it has to end with a slash in order to match the pattern. Changing it to (.*?)/?$ makes it optional.

.htaccess mod_rewrite and Query Strings

In my .htaccess file I've got a rule that strips the .php:
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]
So, /mydomain.com/page.php becomes /mydomain.com/page/ - Which works fine.
Now I'm trying to achieve something similar with query strings, where:
/mydomain.com/page.php?variable=value becomes /mydomain/page/value/
I've tried numerous methods and the best I can get is /mydomain/page/?variable=value
Your second rule is
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
You can change this to rewrite to a query param like this:
RewriteRule ^([^/]+)/([^/]+)/$ /$1.php?variable=$2

mod-rewrite force ?

I need to be able to post links that look like "mysite.com/?d=48YSWD96" & go to file path "/d=48YSWD96.php" So, I need "mysite.com/?d=48YSWD96" to go to "mysite.com/d=48YSWD96". I can't start my file name with a "?" because it will just turn into an index page for the root folder and disregard the content in the file. How do I do this? this is my htacces:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -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]
RewriteBase /
Same thing as the query string match from this question (which has the same .htaccess): url construction htaccess
RewriteCond %{QUERY_STRING} (^|&)d=([A-Za-z0-9]+)($|&)
RewriteRule ^$ /d=%2.php [L]

Resources