URL Rewriting Help - .htaccess

I'm trying to learn Regex & URL Rewriting in PHP. How can I create an .htaccess file which will rewrite (not redirect) any url to index.php?q={url}?
For example:
http://www.example.com/baloon
to
http://www.example.com/index.php?q=baloon
I tried:
RewriteEngine On
RewriteRule ^$ index.php?q=$1 [R]
...but it is not working. What am I doing wrong?
Thanks.

Rewriting ANY url to index.php?q=$1 would result in internal server error as it'd create an endless loop; instead do something like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php?q=$1 [L]

Related

.htaccess help for redirect

Need some help trying to redirect a domain:
https://www.example.com/blog/page/10/?page_id=%2Ffeed%2Fatom%2F to /blog/
I think the best way is to just redirect anything after /page
The code below is what I'm thinking but I know is not correct:
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/page/[0-255]* [NC]
RewriteRule ^ /blog/? [NC,R=301,L]
Any advice?
Need some help trying to redirect a domain:
If you are trying to redirect the domain use something like
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteRule ^(.*)$ "http://www\.exampe2\.com\/blog/" [R=301,L]
If you want to append the request to the URL use $1
If you're rewriting the path to /blog/ and want to keep the query string use the [QSA] flag ie.
RewriteRule /blog/(.*) /blog/ [R=301,QSA,L]
this will make sure you keep your query string.

Redirect dirty urls to a clean url

Alright so I have done the process of cleaning up my urls and everything is going good I put the following in my htaccess.
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /l/index.php?mp3=$1&img=http://www.dailynewsounds.com/wp-content/uploads/2016/01/tinashe-5535799554a1b-3.jpg&g=R\%26amp\%3BB&p=8\%20hours\%20ago&s=+Energy&a=Tinashe+Ft.+Juicy+J+ [L]
And this works to change my urls but we have our nasty urls directing to the pages.
http://dl.example.com/l/index.php?mp3=oo8aey7qw3n9&img=http://www.example.com/wp-content/uploads/2016/01/stuey-rock-ft-sy-ari-da-kid-569e83c69cf5e.jpeg&g=R%26amp%3BB&p=10%20hours%20ago&s=+Me+And+You&a=Stuey+Rock+Ft.+Sy+Ari+Da+Kid+
How can I make it so that I can check my url to see if it contains index.php and if it does redirect it to the new page?
Try the following rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /l/index\.php\?\S*mp3=\S* [NC]
RewriteCond %{QUERY_STRING} \bmp3=([^\s&]+) [NC]
RewriteRule ^ /%1.html? [R=301,L]
If you're using Apache 2.4.x, then replace the L] with L,QSD].

Syntax with .htaccess URL rewrite

I need to get the following URL rewrite syntax correct and am struggling.
xxxxx.com/public_html/food/
Needs to be rewritten as:
xxxxx.com/food/
I tried this and it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public_html/food/(.*)$
RewriteRule ^(.*)$ http://xxxxx.com/food/%1 [R=301,L]
My client is using Joomla (which I am not familiar with), so I need to do so using the .htaccess file per everything I have researched so far. I am just struggling getting the syntax to work correctly though.
Thanks!
Try this, .htaccess
for Rewrite
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [QSA,L]
for Redirect (add R=301)
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [R=301,QSA,L]
Edit:
If you want to rewrite all urls (including /food)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
It will allow you to access site like,
http://xxxxx.com/food/ will point to http://xxxxx.com/public_html/food/
http://xxxxx.com/sample/ will point to http://xxxxx.com/public_html/sample/
http://xxxxx.com/anything/cool/?product=123 will point to http://xxxxx.com/public_html/anything/cool/?product=123/

Remove file name in a URL

How can i remove a file name in url with mod_rewrite.
ex: searchpage-some+search.html in to some+search.html
This is my .htaccess code code.
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /searchpage-search-%2.html? [R,L]
#Internal rewrite
RewriteRule searchpage-search-(.*)\.html$ searchpage.php?search=$1 [L]
This is for a search form that uses $_GET request. This works well only thing is that i want to remove the file name. I would really appreciated if anyone can help out.
In your example you have searchpage-some+search.html but in your htaccess you are actually rewriting it to searchpage-search-some+search.html.
But to remove the searchpage-search from links, just remove searchpage-search- from the RewriteRule. Your htaccess would be something like
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /%2.html? [R,L]
#Internal rewrite
RewriteRule (.*)\.html$ searchpage.php?search=$1 [L]

How do i rewrite this url address

I have a URL that looks like this http://domain.com/index.php/view/test. How do i rewrite it so it doesn't have index.php/view in it. I just want it to look like http://domain.com/test. I want users to be able to type that short url directly in the browser
I tried the following in .htaccess but it's not working
RewriteEngine on
RewriteRule ^/$ index.php/view/
I'm running php 5.2.17
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index.php/view/(.*)$
RewriteRule ^(.*)$ index.php/view/$1 [L]
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php/view/$1 [L]
Would do it if I understand you correctly.

Resources