vanity url with .htaccess in php - php

I'm trying to create a vanity url for where users profile could easily be viewed by others.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://localhost/test/public/profile.php?username=$1 [NC]
This is the code but each time i use this code in my .htaccess file, i get 500 Internal Server Error and the sub-folder public disappears from my directory. What am i getting wrong? Do i need any to do any configuration first?

It's better to use global rewrite logic:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
and in Your index.php catch $_SERVER['REQUEST_URI'] and if user exists so respond if not then pass processing to next procedure.
But if You insist on realization as in question so try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?username=$1 [L,QSA]

Check this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^profile/([a-zA-Z0-9_\-/]+)$ test/public/profile.php?username=$1 [L,NC]
</IfModule>
However note:
You must have the rewrite engine on to make it work (google a bit for the installation procedure the OS/system you are working)
Put proper RewriteBase directive if you use it from subdirectory.

Related

How can I remove a single get parameter and change it to just a slask in PHP?

So my url is like this: http://localhost/gender/?name=robert
What I wanted to do is to remove the name value pair and make it just like http://localhost/gender/robert
I googled and got this one https://stackoverflow.com/questions/21523311/removing-get-parameter-in-htaccess but it seems to complicated for me.
Any help would be much appreciated.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?name=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
</IfModule>
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^robert$ gender/?name=robert&%{QUERY_STRING} [NC,L]
If you want this to work for all pages i.e. /any-page gets served as index.php?page=any-page then use
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ gender/?name=$1&%{QUERY_STRING} [NC,L]
How do these rules work?
A RewriteRule has the following syntax
RewriteRule [Pattern] [Substitution] [Flags]
The Pattern can use a regular expression and is matched against the part of the URL after the hostname and port (with the .htaccess placed in the root dir), but before any query string.

Multiple rewrite_rules

I am having a hard time getting two rewrite rules to work. Right now these links do work:
http://domainname.com/blog/something/something/andsomethingelse/
http://domainname.com/blog/
http://domainname.com/leaderboard/
If I access, /leaderboard/something, it shows Not Found error page from WordPress blog, ignoring the leaderboard rewrite rule.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /leaderboard/index.php [L]
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Ignore real files/folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .? - [L]
RewriteRule ^blog/(.+?)$ /blog/index.php [L] # WP standard syntax
RewriteRule ^leaderboard/(.+?)$ /leaderboard/index.php?var=$1 [L]
You should write rewrite_rules like this code:
RewriteRule (.*)/(.*)/(.*)/(.*)$ index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L]
Your writing code matches anything.
I believe this may be what you're looking for.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog/(.+)$ /blog/index.php?path=$1
RewriteRule ^leaderboard/(.+)$ /leaderboard/index.php?path=$1
Then you can access the pages like so:
http://domain.com/blog/something/somethingelse
http://domain.com/leaderboard/something/somethingelse
and in the background it would look like this:
http://domain.com/blog/index.php?path=something/somethingelse
http://domain.com/leaderboard/index.php?path=something/somethingelse

Htaccess : making Fake folder to get accessible/

I have created the folder http://domain.com/sport/
What I need it to do now is show contents of http://domain.com/sport/index.php whenever I access http://domain.com/sport/anything/anything/.
Right now its showing contents of http://livesports.pw/index.php
I tried code below, but it's not working.
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\/$ $1/ [R=301,L]
Note : The folder /sport/ is just an example folder. It can vary. Even if more folder are created, it
must work even for the new ones.
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /index.php [L]
RewriteRule ^([^/]+)/(.+?)/?$ /$1/index.php [L]

mod_rewrite if not a certain page

I have a .htaccess file with the typical wordpress mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
However I need to allow a certain page to not get redirected. that being 'domain/ee'
I tried adding:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}%{REQUEST_URI} [L]
to the top. As I read it, that should have checked to see if the page existed and if so not run the reset of the rewrite. However it's not working.
any ideas? thanks.
Make the first rewrite rule something like this:
RewriteRule ^(path/to/page)$ $1 [L]
These two rules should prevent rewriting any actual files or directories:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

TinyMCE conflicting with htaccess

I have configured my server using htaccess so that I can route all URL's into a PHP $_GET parameter. I want the URLs on the site to finish with .htm. Currently I have this htaccess, and I have never had any problems with it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ajax/([A-Z,a-z,0-9,\_/]*).htm$ index.php?route=$1&opformat=ajax [QSA]
RewriteRule ^([A-Z,a-z,0-9,\_/]*).htm$ index.php?route=$1 [QSA]
However I have just installed TinyMCE which uses .htm files in the js folder e.g. "/js/tinymce/prop.htm". For some reason when this URL is entered my htaccess routes it to php and it doesn't work.
I was given to understand that RewriteCond %{REQUEST_FILENAME} !-f should stop this from happening. Why is it not? RewriteCond %{REQUEST_FILENAME} !-d is working as I can go to "/js/tinymce" in this example and see directory listing.
Thanks.
I suggest you to change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^ajax/([^.]*)\.htm$ index.php?route=$1&opformat=ajax [L,QSA,NC]
RewriteRule ^([^.]*)\.htm$ index.php?route=$1 [L,QSA,NC]

Categories