Htaccess Mod Rewrite issue - .htaccess

I want the following url:
http://localhost/new/post?url=sample-post-one
to Look Like this via htaccess mod_rewrite:
http://localhost/new/post/sample-post-one/
This might be a question asked already or a similar one but I have been trying to figure it out since a couple of hours and did not get to a solution.
Any help will be highly appreciated. Thanks!
Update
Here's what I've tried
<Files .htaccess,.svn> order allow,deny deny from all </Files>
Options +FollowSymlinks
# For Removing .php extension from files
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Rewrite rule
RewriteRule ^post/([a-zA-Z0-9_-]+)/?$ post?url=$1 [L]

This should work :
RewriteEngine on
RewriteRule ^post/([^/.]+)/?$ post?url=$1 [L]
Or better ...
RewriteEngine on
RewriteRule ^post/([a-zA-Z0-9_-]+)/?$ post?url=$1 [L]
By the way, you should have a look to guides like this one.

You can use below code:
http://localhost/new/post?url=sample-post-one
RewriteRule ^post/([0-9]+)/?$ post?url=$1 [NC,L] # Handle post requests
and to get more information you can refer to this

Basically we're capturing the bit for the last folder. The rest can be hard coded. Also you should delete the competing rule. ^ indicates beginning of string. $ is the end of the string. If you use both of those characters everything has to literally match except for the bits where we're using REGEX patterns for matching. Also everything is case sensitive.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^new/post/([^/]+)/?$ new/post?url=$1
If you know that you're only going to allow letters, numbers, periods, and hyphens, then you can do this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^new/post/([^A-Za-z0-9.-]+)/?$ new/post?url=$1
If for some reason you're including new/post and post is really your document root it should look like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^A-Za-z0-9.-]+)/?$ ?url=$1
Apache is a well documented application. They provide an excellent resource with hundreds of examples on their site with explanations of each in the mod_rewrite documentation.

Related

htaccess rewrite key/value pair params in url

I know this is very common question but I can't seem find any solution regarding in my problem. The folder structure looks like this:
- rootProject
- subfolder
* .htaccess
* index.php
Normal url params: http://website.com/subfolder/?dynamicKey=dynamicValue.
But What I want to achieve is like this: http://website.com/subfolder/dynamicKey/dynamicValue
But I failed to do that, I can only retrieve the dynamicKey.
Here is my current htaccess:
RewriteEngine On
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
Thanks to #Ar Rakin. Here is the solution.
RewriteEngine On
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
RewriteRule ^([a-z]+)/([a-z]+)/?$ index.php?$1=$2 [NC,QSA,L]
With your shown samples and attempted code, please try following htaccess rules. We need to add conditional checks before rewrite rules else later it will affect your existing pages also.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##Added conditions if non-existing pages requested then only perform rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
##Added conditions if non-existing pages requested then only perform rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)/([a-z]+)/?$ index.php?$1=$2 [NC,QSA,L]
NOTE: your used regex [a-z] will only match small letters in uri, in case it needed to check any alphabets(capital OR small) then change FROM [a-z] TO [a-zA-Z] in above rules also.

Rewrite URL with just company name with current htaccess file

First I like to say I feel so privileged being here. I have used the answer given here for many years and it has saved me many many hours. I have searched for the answer to my current question with no luck. I believe the answers didn't work because I am adding it to my current .htaccess file which is required. I have asked in my current software program (phpfox) forum but they said it could not be done. I will leave it to stackoverflow experts to tell me whether it can be done or not. Here is my current .htaccess file
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(file)/(.*) PF.Base/$1/$2
RewriteRule ^static/ajax.php index.php
RewriteRule ^themes/default/(.*) PF.Base/theme/default/$1
RewriteRule ^(static|theme|module)/(.*) PF.Base/$1/$2
RewriteRule ^(Apps|themes)/(.*) PF.Site/$1/$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Here are three examples of the URL for this business page.
DomainName/directory/detail/302/name-of-business/overview/
DomainName/directory/detail/302/name-of-business/aboutus/
DomainName/directory/detail/302/name-of-business/contactus/
I think you get the picture but just in case here is a different business.
DomainName/directory/detail/303/name-of-business/overview/
Now for this business page the /directory/details never changes. The 302 is the primary id of the record in the database table for this business. In that same record is the name-of-business. So if I type in the browser URL DomainName/directory/detail/302 it will still bring me to main overview page even without "name-of-business/overview" at the end of the URL. So I am assuming the php code throws that at the end of the url from the database depending on what menu you click on this specific business.
My dream hope is getting it down to
DomainName/name-of-business/overview
DomainName/name-of-business/contact etc...
But I would even be happy to get rid of at least directory/detail (I am guessing the id "302" is needed since it is the main identifier...just guessing).
DomainName/302/name-of-business/overview
I have tried many different answers here but I feel I also may be putting it on the wrong line in my current .htaccess file. Thank you in advance for any help and your time.
Try with:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(file)/(.*) PF.Base/$1/$2
RewriteRule ^static/ajax.php index.php
RewriteRule ^themes/default/(.*) PF.Base/theme/default/$1
RewriteRule ^(static|theme|module)/(.*) PF.Base/$1/$2
RewriteRule ^(Apps|themes)/(.*) PF.Site/$1/$2
# Rewrite DomainName/303/... to DomainName/directory/detail/303/...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+)(/.*|$) index.php?q=directory/detail/$1$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Rewrite DomainName/303/... to DomainName/directory/detail/303/...
but only for external links or once fixed in your pages

URL rewriting breaks when index.php?url=index (parameter is "index"). Why?

Weird issue: I'm using this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
to get these URLs. Works perfectly.
http://www.example.com/cool/url
Problem
But when the ?url-parameter has the word index in it, like in
http://www.example.com/index/page
then $_GET["url"] is empty. I think my RewriteRule is broken (despite i've seen exactly this RewriteRule in lots of tutorials) and the removing of index.php from the URL also removes the index-parameter.
Question
How to fix, how to make URLs like index/page possible ?
This is due to MultiViews option.
Add this line on top of your .htaccess file to disable MultiViews:
Options -MultiViews

Twitter Like URL but with a #'s?

I am making a social networking website, and I can't get this done:
If the url is http://url.com/#username, I want it to show the http://url.com/user?u=username page, but show http://url.com/#username URL.
However, if there is no # in the beginning, treat it like a regular URL.
This is my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/(\d+)*$ ./user.php?u=$1
The last three lines are what I tried, however that does not work as I wanted it to. I thought it would work because RewriteRule ^/(\d+)*$ takes any URI after the slash, and rewrites it to .user.php?u=*, but that didn't work, so I am looking for some suggestions on what to do next.
From my understanding of your question, this should work.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^#(.+?)$ user.php?u=$1 [NC,L]
It requires you to have a # before the username and it passes it as a GET variable (u) However, a person can add other characters which could confuse it as a username.
/#username/muffins
But if you want this, and to have pages per username (ie, /#username/info, /#username/about, etc) you can just use the explode() function in PHP.
If you just want it to get the username and nothing else, you can try
RewriteRule ^#([A-Za-z0-9]+)$ user.php?u=$1 [NC,L]
Hope this helps! :)

Redirecting many folders and many parameters

Need the redirection I will try to explain with an example:
Entered address in the browser url bar:
http://example.com/any/folders/qty/FileName/?any number of key-value pairs separated by &
to be redirected to the following url:
http://example.com/any/folders/qty/FileName/FileName.php?any number of key-value pairs separated by &
Googled for a suitable solution and searched SO but couldn't find anything similar that had a working answer, maybe because most of the questions are pretty confusing. I hope this one is clear enough.
Any help will be greatly appreciated.
You may try this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^.*/([^/]*)/.*$ [NC]
RewriteRule ^(.*)/?$ $1%1.php? [L]
Will map this:
http://example.com/any/folders/qty/FileName/?key1=var1&key2=var2&key3=var3
to this as the resource address:
http://example.com/any/folders/qty/FileName/FileName.php?key1=var1&key2=var2&key3=var3
Use This:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Say your url is:
url=http://www.example.com/page/view?id=10&name=about_us
in index.php
$req=$_REQUEST['url'];
$reqExtracted=explode('/',$req);
$folder=$reqExtracted[0];
$pagename=$reqExtracted[1];
$id=$_REQUEST['id'];
$name=$_REQUEST['name'];

Resources