I've been trying to figure this out for a few hours but stumped. I am trying to rewrite the url with a get variable to make it seem like a file. I've tried many different answers I've seen to no avail. Is it possible that I don't have a certain setting turned on? Your help is greatly appreciated!!
I am trying to redirect
www.site.com/products/?q=coconut
to
www.site.com/products/coconut
so far this code gives me a 500 error
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /products?q=$1 [L,R=301]
Wow dumb mistake on my part, but leaving this up just in case someone else is pulling their hair out. I had a faulty php code which lead to a 500 error.
Related
A while ago I made my first website using only HTMl code and some basic inline CSS.
I don't have the experience and the intuitive knowledge of how things work in this world that is why even the most basic errors I have to solve or tweaks I have to make gives me headache.
My website looks like it's duplicated because when you enter the www or the non www version, then both work and for the search engines it looks like there are two identical websites, which is not good for me.
One tool showed me an error saying: "https://domain.lv and https://www.domain.lv should resolve to the same URL, but currently do not."
I've tried different methods to solve this by editing my .htaccess file, but nothing works. I'm sure there is some dumb mistake I am making because I don't know how should even basic things look like when they are right.
This here is my .htaccess file now (for this I used the word domain as a placeholder for my real domain name):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.lv [NC]
RewriteRule ^(.*)$ https://domain.lv/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
ErrorDocument 404 /404.html
Do you see what is wrong here and how I get it right?
And maybe someone could give me a sample file of how .htaccess file should look like for websites built from scratch?
I will appreciate any help.
Cheers!
New poster to this place but long term user.
I have a site which uses index.php and then has a viewpost.php and categorypost.php.
The viewpost is for viewing the main posts and the categorypost is for viewing posts under a category such as "webdesign".
The view post slug is made up of query string such as:
viewpost.php?postType=services&postCategory=webdesign&postTitle=somepost
I then want that to rewrite to
website/services/webdesign/somepost
First question:
Is this the correct way to do this by using 3 files or am I creating more work than needed?
Second question:
If I'm on the correct or at least an ok path, how do I go about redirecting?
I have seen about 30 or so posts around this but finding it hard to get my head around it and have ended up with 500 response or just straight redirecting.
RewriteRule ^(.*)$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [QSA,L]
Any help is greatly appreciated.
If these particular links can all start with the same word, it is the simplest, in this case "website". Because it avoids mistakenly using an existing folder with a missing page.
RewriteRule ^website/([^/]+)/([^/]+)/([^/]+)/?$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [NC,QSA,L]
But if it's not the case, you can use:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [QSA,L]
recently i have tried to build some MVC application without any framework to understand MVC pattern better. Till now i have resolved every problem i have had BUT....
Pretty common thing is to make your URL looks "nicer"
For example www.somesite.com/controller/method
instead of www.somesite.com/index.php?c=1&m=2.
i achieved this simply with htacces by aiming it to a variable.
in htaccess...RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This works perfectly fine until someone tries to rewrite variable "url".
Basically if someone types www.somesite.com/controller/method?url=1
my Application will pop up an error page because i am parsing everything after / and calling specific controllers and methods by its name (or popping up an error page if that doesnt exists).
So i would like to know if there is a better way to do this or way to avoid this behaviourThanks :)
EDIT
In last few hours i tried to find a better solutions. I thought i could put my url into Enviromental variable instead of into get variable.
So i experimented with commands like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php [E=URL:$1,QSA]
unfortunately $_SERVER["URL"] is blank......
i would be really happy if someone could help me with this piece of code :) Thanks
EDIT 2
Okay to make it clear i'll add few examples.
My current htacces looks like this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA]
So if someone goes to www.somesite.com/foo/bar
i will check $_GET["url"] and then explode it explode("/", filter_var(rtrim($_GET["url"], "/"), FILTER_SANITIZE_URL))
Now i have array which looks like [0=>foo 1=>bar]
That means in my code that i will try to call controller called foo and then method in this class called bar
In case that the user will try to acces www.somesite.com/foo/bar/fee/faa, fee and faa will pass as a parameters to method bar in class foo.
So this was just example how does this work. My problem is as i said when someone tries to acces lets say www.somesite.com/foo?url=0. Then my script will try to handle $_GET["url"] and the result wont be foo but 0 because of ?url=0 rewrites the value of url which was originaly set in my .htacces. So my scripts will try to call controller called 0 and if that doesnt exists itt will popup error404. I have already tried to ignore this specific variable via QUERY_STRING in htacces but this seems to me like a stupid solution. For now i would like to stick with setting evniromental variable instead of get variable or if there is some better way to achieve this :) Thanks
Your current set-up relies on $_GET to obtain core information but, as you've faced, that variable is populated from user input so anyone can mess with your routing, even inadvertently.
A typical Apache configuration for a custom router looks like this (this snippet is from CakePHP/2.x):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
As you can see is doesn't add GET variables of its own, it merely lets existing ones to pass through. Original URL is right there inside $_SERVER, you don't need to instruct Apache to copy it into the redirected URL.
I understand you want $_SERVER['REQUEST_URI'] but you can peek inside the array with the usual methods: print_r($_SERVER), var_dump($_SERVER), phpinfo()...
hello all i just want to rewrite my url for new site.
as you can see i had done it here but there you can see numbers that is id that is automtically generated .
http://www.domainname.com/sndslnf-1412.html
RewriteRule ^(.)/([0-9])$ details.php?pid=$2 [L]
now i'm trying to just show the title no numbers
ex: http://www.domainname.com/sndslnf
can anyone guide i have tried many time but don't know i always get and error .
"The server encountered an internal error or misconfiguration and was unable to complete your request."
here is the htaccess code to
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
thanks in advance for the help.
It seems to be impossible. You have to change the functioning of your system.
To begin, use this htaccess code :
RewriteRule ^(a-z)+$ details.php?pid=$1
Now, the pid parameter is a string rather than a number. Then, you just have to check if this string is in your database, to display the string and informations who correspond to it.
I'm sorry for my bad english and i hope you understand me.
I was wondering how to pass several (two) values through url as a clean url.
I've done clean urls before, but never with multiple values and it doesn't seem to be working.
This is what the url looks like now:
http://example.com/?user=Username&page=1
This is what I want it to look like
http://example.com/user/Username/page/1
I've tried other answers that I've seen on here, but they aren't working for this certain deal.
RewriteEngine On
# Don't match real existing files so CSS, scripts, images aren't rewritten
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# Match the first two groups before / and send them to the query string
RewriteRule ^([^/]+)/([^/]+) index.php?user=$1&page=$2 [L]
Thanks. :)
I'm using PHP by the way. :)
Also, will I still be able to use $_GET with this? I thought so, but I also somewhere else where it said you can't... :D
You're missing several matches, try:
RewriteEngine On
# Don't match real existing files so CSS, scripts, images aren't rewritten
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?$1=$2&$3=$4 [L]
This will take a URL like:
http://example.com/a/b/c/d
to the URI:
/index.php?a=b&c=d
will I still be able to use $_GET with this? I thought so, but I also somewhere else where it said you can't.
In the above example, when you look at $_GET['a'] you'd get b.