Giving error with dot in url - .htaccess

i have a problem with my url, when dot comes in url it gives an 404 error.
url: http://example.com/Samsung-Galaxy-Mega-5.8
i used some code in htaccess but it doesn't work properly.
RewriteRule ^([a-zA-Z_\-]+)/?([a-zA-Z0-9\-=&_#\.]*)$ /$1.php?$2 [QSA,L]
EDIT (copied from comment):
RewriteEngine on
Options -Indexes
#RewriteCond %{HTTP_HOST} ^([a-z0-9-A-Z_]*).([a-z]*)$
#RewriteRule ^(.*)$ %1.%2/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([a-zA-Z0-9_-]+)$ show_mobile.php?bid=$1&brandname=$2
RewriteRule ^([a-zA-Z0-9\-]+)-([a-zA-Z0-9\-]+)$ show_mobile.php?bid=$2&brandname=$1

I guess the following should get you going:
^([a-z0-9\-\.]+)\-([a-z0-9\-\.]+)$i
(based on the .htaccess from comment)
https://regex101.com/r/jZ9sP3/1
You didn't allow for periods and you didn't escape the hyphen inbetween the catching rules.
the tailing i is for case insensitive, so you don't need to do a-zA-Z

Related

Failing to prettify URL with htaccess

I'm in the process of trying to prettify my URLs
From: http://localhost/blog.php?id=1
To: http://localhost/blog/1
I've added the below to my .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^blog/([a-zA-Z0-9]+)$ blog.php?id=$1 [NC,L]
From my understanding that should now redirect when I call my "to" url in PHP header("location: /blog/".id); it takes me to the relevant page, and my $_GET['id'] should have a value. It's coming up empty
I've been messing around with a few different regular expressions, but I don't think that's the issue.
In the apache config I changed "AllowOverride" to 'All'
To check to see if the .htaccess was even being used I put in some invalid regex, and got an error in apache_error.log
I've been on a ton of different pages asking the same thing, and watched plenty of YouTube vids, but I can't see what I'm missing
Have your htaccess Rules file in following manner. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews
RewriteEngine On
##Rules for external redirect to blog/1 here.
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(blog)\.php\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rules for internal rewrite to blog.php file here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([\w-]+)/?$ blog.php?id=$1 [QSA,NC,L]
##Rules for internal rewrite to index.php file here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ index.php?id=$1 [QSA,NC,L]
try this .
1.
Options +FollowSymLinks
RewriteEngine on
RewriteRule blog/id/(.*)/ blog.php?id=$1
RewriteRule blog/id/(.*) blog.php?id=$1
example url :- http://localhost/blog/id/1/
Directory Type URL

URL Rewriting with "!" (exclamation mark)

I am stuck with this challenge, and I really hope that someone can help me out.
I am currently working on an API, and a friendly URL is needed to call on the API.
A VOLKSWAGEN manufacture has released a car called UP!
If you notice there is an exclamation on the brand type. This has created an issue with the .htaccess because it lands to 404 page.
Sample URL:
http://mysite.com/cars/new/VOLKSWAGEN/UP!
Has anyone ever encountered this issue, and solved it?
This is my .htaccess file
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Many thanks.
Insert this rule just below RewriteEngine On line:
RewriteRule ^(.*?)!$ /$1 [R=301,L]
This will remove trailing ! from all URIs.
You need to be using URL encoding. I'm not quite sure what you mean by "friendly", but you simply can't have an exclamation-point character in an HTTP URL. Most browsers these days are translating encoded characters into their equivalents on the address bar, though.

RewriteRule do THIS if image file, or else do THAT

I'm having a hard time having this to work..
I have installed YOURLS wich is a PHP script to shorten urls.
In order to work, it needs to have this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourls-loader.php [L]
No problem here.
But I also want to use a directory for image hosting that has nothing to do with the PHP script.
It would check if the requested url ends with .jpg|.jpeg|.gif|.png and RewriteRule would redirect to /imgshare/$1
I've tried the code below but I get a server error when going to mysite.com/img.jpg but not for the url redirection "mysite.com/y4Jd":
RewriteCond %{REQUEST_URI} !(\.jpg|\.jpeg|\.gif|\.png)$ [NC]
RewriteRule ^(.*)$ /yourls-loader.php [L]
RewriteCond %{REQUEST_URI} (\.jpg|\.jpeg|\.gif|\.png)$ [NC]
RewriteRule ^(.*\.(jpeg|jpg|png|gif))$ /imgshare/$1 [L]
This is not the issue, but as a note, your second RewriteCond already matches files with image endings, so there's no need to repeat that match in the RewriteRule. Alternately, there's no need for the RewriteCond since it's redundant to the RewriteRule.
The real issue, however, may be that you have an extra slash in the final rule. $1 will contain the leading slash matched from the original URL so your rule is currently adding two slashes between imgshare and the file name. I would implement the rule like this:
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|gif|png)$ [NC]
RewriteRule ^(.*)$ /imgshare$1 [L]

Redirect All Requests to a Post

I'm trying to redirect all pages to a blog post at URL
example.com/big-changes-for-2013/
(including trailing slash)
I do not want to redirect me, because I'm working on the rest of the site.
This is what I have so far (this is a .htaccess redirect):
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/big-changes-for-2013/$
RewriteCond %{REMOTE_ADDR} !^50\.137\.88\.129
RewriteRule $ /big-changes-for-2013/$ [R=302,L]
The part preventing me from being redirected works. The part thats not working is the redirect itself, which is an infinite loop.
The code above is based off of a combination of this and this.
Any ideas?
Try
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^50\.137\.88\.129
RewriteRule ^(?!big-changes-for-2013/$) /big-changes-for-2013/$ [R=302,L]
The rule regexp is what is a called a negative look-ahead assertion. It means "match anything other than big-changes-for-2013/$".
Solved with:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REMOTE_ADDR} !^50\.137\.88\.129
RewriteCond %{REQUEST_URI} !^/(big-changes-for-2013/)
RewriteRule ^(.*) /big-changes-for-2013/ [L,R=301]
</IfModule>

.htaccess Mod Rewrite code?

I have searched for this but nobody have the same issue..
Now I need an code to transform the ugly url (thats what i have) to the nice url:
Ugly url: domain.com/aanmelden?referral=admin
Nice url: domain.com/aanmelden/admin
Ik have tried soo many codes, but no one did work for me. ):
suggestions?
Thnx!
(My current htaccess: )
ErrorDocument 401 /errordocs/401.php
ErrorDocument 403 /errordocs/403.php
ErrorDocument 404 /errordocs/404.php
ErrorDocument 500 /errordocs/500.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.nl$ [NC]
RewriteRule ^(.*)$ http://domain.nl/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
Where and what i need to place?
EDIT:
To be clear:
If the user goes to the Nice Url, they wil stay on the nice one.
If the user goes to the Ugly Url, they wil redirect to the Nice url.
This is a trivial rewrite:
If admin can be any lowercase string, use the pattern ([a-z]+)$ to capture one or more lowercase letters after the / and before the end of the request URI.
RewriteEngine On
RewriteRule ^aanmelden/([a-z]+)$ aanmelden?referral=$1 [L]
If admin is really only admin, you can hard-code it as
RewriteEngine On
RewriteRule ^aanmelden/admin aanmelden?referral=admin [L]
Update
In context of your existing rewrites just posted, you'll need to add this rule before the rule that adds .php since that would also match this pattern.
# Do this before the rule that adds .php
# Also added condition so this doesn't apply to real files...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^aanmelden/([a-z]+)$ aanmelden?referral=$1 [L]
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Update 2
If you want the end user to get the ugly URL and rewrite it to the nice one, use:
RewriteCond %{REQUEST_QUERYSTRING} referral=([a-z]+)
RewriteRule ^aanmelden aanmelden/%1 [L]

Resources