Change URLs with .htaccess - php

I have a website that I'm trying to change the URLs on. All of the URLs start with http://domain.com/?
For example, http://domain.com/?index
I just want to remove the question mark. I don't care if it appears in the address bar, I just want my users to be able to access the pages on the site without having to type the question marks.
So, if a user wants to access http://domain.com/?index, I want them to be able to access it by typing http://domain.com/index.
Is this possible using .htaccess?
I've searched around and tried a few different things for a few days now and still can't figure out a way to accomplish what I'm trying to do.
Any help is appreciated.
Thanks.

Try:
RewriteEngine On
# Match against the request instead of the URI
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^&\ ]+)&?([^\ ]*)
RewriteRule ^$ /%1?%2 [L,R=301]
This takes URI's like http://example.com/?path/to/file.txt and redirects the browser to http://example.com/path/to/file.txt. The browser will display that URL in the location bar instead. This is, of course, assuming that if someone actually goes to that URL, that there is something there to be served other than a 404.
EDIT
To internally map none-query string URLs to the one with a query string:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?$1 [L]

Try using :
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ /?$1 [NC]
in your .htaccess file ,
let me know if it works.

Related

.htaccess redirect query to url

so I have a php query url like so
https://domain.tld/dl.php?device=dumpling
now I would like to be able to go on a nicer url like below to also redirect at same url as above
https://domain.tld/dl/dumpling
so by accessing both urls, would get same page
I know this is possible via apache .htaccess yet can't seem to figure out the proper values there
what I tried so far is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?dl/(.*?)/?$ /dl.php?device=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /dl\.php\?device=([^\&\ ]+)
RewriteRule ^/?dl\.php$ /dl/%1? [L,R=301]
Now by accessing https://domain.tld/dl.php?device=dumpling I do get redirected to https://domain.tld/dl/dumpling, however this returns a 404 page
Think it tries to go on /dl/dumpling folder that is indeed missing
I want only https://domain.tld/dl/dumpling to work and also return my query
What the heck am I doing wrong here?

htaccess RewriteRule - Removing '/' From The URL Causes Request To Display In URL

I have a little problem concerning .htaccess.
The problem lies more in aesthetics than functionality.
I have a very small htaccess file which contains the following, and nothing more:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9\/\-]*)$ index.php?request=$1 [QSA]
This sends any requests to index.php, parsing in the URI. I then manipulate/read the URI to get the relevant file.
An example URL would be this:
http://localhost/domain/fixtures/2015/
Now, if I was to remove the end '/', and hit enter, the URL would change to:
http://localhost/domain/fixtures/2015/?request=fixtures/2015
I have tried adding a '/' to the rewrite but it does nothing.
I have searched across Google to no avail. (Bearing in mind some of my searches went along the lines of, "/ added to url htaccess rewrite")
The main reason I want to get this sorted is because it doesn't look pretty, but also because it creates duplicate content (i.e. you can get to the same page from both URL), which is not good for SEO.
Any direction I can be pointed in is a great help.
Cheers
If I am reading this right, you want external URLs in this form:
http://localhost/domain/fixtures/2015/
And NOT this form:
http://localhost/domain/fixtures/2015
In order to achieve this, you need to use a redirect to let the remote
client know that the latter form is not valid, e.g.,
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* %{REQUEST_URI}/ [R=301,L]
If you are also serving static content, you might want this as well:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* %{REQUEST_URI}/ [R=301,L]
So, the full recipe including your PHP content generator:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([a-zA-Z0-9\/\-]*)$ index.php?request=$1 [QSA]

Redirecting existing URLs to SEO Friendly URLs

I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1

Redirect to a URL with .htaccess

My .htaccess file currently looks something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]*)/$ ?page=$1 [L]
It makes URLs friendly from something like domain.com/?page=2 to domain.com/page/2/.
However, is there a way that I can edit this .htaccess file to make it redirect URLs containing ?page=2 (or any page number) to it's nicely formatted URL (/page/2/) automatically? My current one just allows the friendly URL version to "exist", but in no way enforces a redirect to it.
Also, is there a way I can redirect ?page=1 or /page/1/ just to the main directory/home?
EDIT:
After using what I received as an answer from Jon Lin, I was able to solve the second part of my question. Considering that ?page=1 automatically redirected to /page/1/, all I had to do was redirect /page/1/ to the homepage:
RewriteCond %{THE_REQUEST} \ /+DIRECTORY/(?:page|)/1/?
RewriteRule ^ /DIRECTORY/? [L,R]
Add this right below the RewriteEngine on line:
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)\?page=([0-9]+)
RewriteRule ^ /page/%1/? [L,R]

Having trouble with a simple .htaccess redirect

I've been working on this for a while and have tried a lot of different solutions I've seen on the web and can't seem to get this to work.
I have a site at www.mydomainname.com. The page that I want to handle ALL page requests is www.mydomain.com/index.php. I'd also like to set this up to work for any other domains that I point to this code base (using wildcards would be the way to go for that I think).
So the following URL types (or any other) should automatically go to index.php, while still keeping the original URL structure in the browser address bar:
www.mydomain.com/
mydomain.com/
www.mydomain.com/item/111
www.mydomain.com/item/itemname/anothervariable/value
www.mydomain.com/item/itemname/?variable=value
I'm using PHP 5 and a recent version of Apache with mod_rewrite enabled.
Thanks in advance for any help!
Simple:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !\.(jpg|gif|ico|png|bmp|css|js)$
RewriteRule .* index.php
You could use the follow RewriteRule
RewriteEngine On
RewriteRule ^(.*)$ /index.php?originalUrl=$1
Untested, but it should work. You will then also have the original URL available in the 'originalUrl' GET parameter for further parsing.
Include this once per .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule (.*) index.php
If you need the information from the matched URL you can modify your RewriteRule to match portions of the old URL or just include everything by using the variables $1 and so forth. If for instance you wanted to get the item number passed in quickly to index.php, you could use this rule:
RewriteRule item/(.*)$ index.php?item=$1

Categories