Redirecting many folders and many parameters - .htaccess

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'];

Related

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

How to use RewriteCond to get rid of duplicate page

Hello guys! I need some help with htaccess, because I'm really stuck with it. I have some pages, like:
site.ru/books
site.ru/pens
And those links can be called by other urls:
site.ru/index.php?show=books
site.ru/index.php?show=pens
I hope you understand those links above are equal. Just, urls are user friendly.
I want to use RewriteCond to make a redirect from "site.ru/index.php?show=books" to "site.ru/books", because Google or other Search Engines don't like duplicate pages.
But the problem is I don't know how to write this… Help me please, and sorry for my bad English ;-)
put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.php\?show=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?show=$1 [L,QSA]

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! :)

Htaccess Mod Rewrite issue

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.

mod-rewrite forwarding without changing URL

I have a small problem with my Apache configuration when creating "pretty" URLs. I've got it to the stage where typing (or linkig for that matter) to
index.html
forwards you to
index.php?pageID=Forside
that is exactly what I want.
But how can I get index.html to stay in the address bar of the browser?
Right now it forwards and changes the URL to the original one.
Here my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} index\.html
RewriteRule .* http://www.radoor-designs.dk/index.php?pageID=Forside [L]
And before someone comments on it: Options +FollowSymLinks is missing since it triggers an error 500 on a one.com webhotel.
Thanks in advance!
Try the following:
RewriteEngine On
RewriteRule ^index\.html$ /index.php?pageID=Forside [L]
I think this may help you to resolve your problem.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html$ /index.php?pageID=Forside [L]
This will do the redirect for you whilst showing index.html in the browser window.
Strange that symbolic links creates an error 500,
if you want it to redirect to index.html?pageID=Forside then do
RewriteRule .* /index.html?pageID=Forside [QSA,L,R=301]
I'm not 100% certain what you are trying to achieve with this could you explain a little more?

Resources