.htaccess Pretty URL rewrite for PHP pages in a subfolder? - php

I've got the following directory setup:
http://www.mysite.com/public/
I'm using the following rewrite to remove the 'public' folder from visible URLs:
#rewrite the URL to display the subdirectory as the main directory for all links
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com
RewriteCond %{REQUEST_URI} !^/public
Rewriterule ^(.*)$ /public/$1 [L]
This works great, and redirects everything correctly. However, I also want to rewrite some of the dynamic pages that live in the 'public' subfolder as well, but am having trouble getting any of the rewrites I've found to work in conjunction with the above rule.
For example, with the above subdirectory rewrite rule in place, going to a URL like:
http://www.mysite.com/item.php?id=1&name=item_name
...should be rewritten to something like:
http://www.mysite.com/items/item_name
Thoughts?

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
#rewrite the URL to display the subdirectory as the main directory for all links
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
Rewriterule ^(?!public/|prints/)(.*)$ /public/$1 [L,NC]
RewriteCond %{THE_REQUEST} /*item\.php\?id=([^&]*)&name=([^&]*)\s [NC]
Rewriterule ^ /prints/%1/%2? [R,L,NC]
Rewriterule ^prints/([^/]*)/([^/]*)/?$ public/item.php?id=$1&name=$2 [L,NC,QSA]
PS: Make sure your static includes like css, js, images etc have absolute path rather than a relative one.

Related

.htaccess URL rewrite rules folder

I have this in my html folder
--html
--.htaccess
--app
--application_name
--public
--index.php
--landing_page
--index.php
As of now, everytime the website url is accessed (www.website.com), the browser opens the landing_page/index.php.
How can I make it that when I access www.website.com/beta, I will be doing a request to html/app/application_name/public/index.php
I have tried adding this to the .htaccess
RewriteRule ^beta/?$ app/application_name/public/index.php [L,NC]
This is the whole .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^beta/?$ app/application_name/public/index.php [L,NC]
But it does not work. Not sure what term to use to search for this kind of problem.
Your root is currently set at /html, and therefore the RewriteBase / is at /, the /beta/ simply does not exist. You could change the RewriteBase /app/application_home/public/ and set the RewriteRule ^beta/?$ index.php [L,NC]. This however no longer give you the access to the landing page, not sure if this is what you want?

htaccess wildcard subdomain rewrite subfolder causing loop

We are using a wildcard subdomain, *.subdomain.com Each state has its own directory inside the /state/ directory, and within that directory there is a state.html. The URL would be domain.com/state/$state/state.html which would then be rewritten to show domain.com/state.html?state=$state
All works good but now we need to go from subfolder to subdomain so the ending result would be either
$state.domain.com - Preferred but not necessary
or
$state.domain.com/state.html?state=$state
What we have currently is below, but we get a redirect loop.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain.com
RewriteCond %{HTTP_HOST} ^(.+).domain.com
RewriteRule ^([^/]*)$ /state/%1 [P,L,QSA]
#Rewrite Rule - Force state and city lookup to use state.html or city.html and use info from url to define which city and state.
RewriteRule ^state/(.*)$ state.html?state=$1 [L]
RewriteRule ^city/(.*)/(.*)$ city.html?city=$1&state=$2 [L]
Ended up using the following which works
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain.com
RewriteCond %{HTTP_HOST} ^(.+).domain.com
RewriteRule ^([^/]*)$ /state/%1/state.html?state=$1 [L,QSA]

URL Rewriting (Internal/External Forwarders) .htaccess

I am using .htaccess for URL Rewriting. My current .htaccess code is:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+fr/transporter/transporterPublicProfile\.php\?profil=([^\s&]+) [NC]
RewriteRule ^ fr/profil-des-transporteurs/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^fr/profil-des-transporteurs/([^/.]+)/?$ fr/transporter/transporterPublicProfile.php?profil=$1 [L,QSA,NC]
My URL is :
https://example.com/fr/profil-des-transporteurs/1927
It works fine when I place .htaccess file in DOCUMENT_ROOT directory.
But it doesnot work when I put same .htaccess file in real sub-directory i.e /fr/.
So what should I do, if I want to place .htaccess file in sub-directory i.e /fr/
Use these rules inside /fr/.htaccess:
RewriteEngine On
RewriteBase /fr/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /fr/transporter/transporterPublicProfile\.php\?profil=([^\s&]+) [NC]
RewriteRule ^ profil-des-transporteurs/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^profil-des-transporteurs/([^/.]+)/?$ transporter/transporterPublicProfile.php?profil=$1 [L,QSA,NC]
Try this
RewriteEngine On
RewriteBase /fr

.htaccess Rewrite Parent Folder to Subfolder

I am looking to rewrite the url so if someone lands on a parent folder, they will be redirected to its subfolder. http://www.domain.com/portfolio should take me to http://www.domain.com/portfolio/example
RewriteEngine is on, and there are other RewriteRules in the file, which are working. I have tried the following rewrites, but none perform the redirect:
RewriteRule ^/portfolio/(.*)$ /portfolio/example/$1 [R=301,L,QSA]
and
RewriteCond %{HTTP_HOST} ^(www.)?domain.com/portfolio$
RewriteRule ^(/)?$ portfolio/example[L,QSA]
and
RewriteRule ^portfolio/(.*) http://www.domain.com/portfolio/example/$1 [R=301,L,QSA]
and
RewriteRule ^portfolio$ http://www.domain.com/portfolio/example[R=301,L,QSA]
RewriteRule ^portfolio/(.*) http://www.domain.com/portfolio/example$1 [R=301,L,QSA]
What is the proper syntax for rewriting a url so that if a visitor lands on the parent portfolio folder, they go to the portfolio/example subfolder?
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^portfolio(/(?!example).*|)$ /portfolio/example$1 [R=301,L,NC]

.htaccess - Redirect subdomain to folder

This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.
I simply want m.daltonempire.nl to be redirected to daltonempire.nl/m/ without the user seeing the URL change.
So if m.daltonempire.nl/hello.php is requested, I want the user to keep seeing this URL, while the page given is actually daltonempire.nl/m/hello.php.
Note: I do not want www., so simply http://m.daltonempire.nl
Thanks in advance,
Isaiah v. Hunen
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/$1 [L]
The %{REQUEST_FILENAME} conditions would let you access /exists.php and not rewrite it to /m/exists.php. Remove those two if you want to rewrite even if that may potentially override existing files and directories.
Try this example:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]
Any requests http://test.example.com will be mapped to http://example.com/test/...
Try googling dynamic subdomain with php and htaccess to get better search results.
I have set CNAME of my sub domain below:
blog.mydomain.com
to my wordpress that installed in folder /blog/ under root directory.
Formerly I need to use this url to call wordpress:
http://blog.mydomain.com/blog/
which is ugly. I have tried many code to redirect:
http://blog.mydomain.com/
to the folder so I can use it as my wordpress url.
Finally I got .htaccess setting that is work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1
I have also CNAME other subdomain: http://forum.mydomain.com to mybb installation in folder /forum/mybb/ so the .htaccess need to put [L] on each of RewriteRule code as below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/forum/mybb/
RewriteRule (.*) /forum/mybb/$1 [L]
RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1 [L]
In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.

Categories