.htaccess to create SEO url not working - apache

I have the file path: example.com/blog/ where blog is a directory. Blog gets a query string from the URL called URL and makes a dynamic page out of that information. So essentially it's something like this: example.com/blog?url=hello-world but i'd like to remove the ?url part and instead add a slash. So it should look something like this: example.com/blog/hello-world I've tried to accomplish this by putting the .htaccess file in the blog directory. This is my current .htaccess file, but it is not working:
.htaccess
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+) [NC]
RewriteRule ^ %1? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1 [L,QSA]
Inside /blog there is a index.php file and that generates the dynamic page.

RewriteEngine On # Turn on the rewriting engine
RewriteRule ^blog/([A-Za-z0-9-]+)/?$ blog/index.php?url=$1 [NC,L]
RewriteRule ^blog/?$ blog/index.php [NC,L]

Try this one.
EDIT
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(blog)/(.*)$ blog/index.php?url=$1

Related

shorten url with htaccess

I have an old system and would like to shorten the URL.
At the moment:
www.site.com/app/views/accessories.php
I would like to open it like this:
www.site.com/accessories.php
I tried to do like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteRule ^(.*)$ app/Views/$1 [NC,QSA]
But this error appears "Internal Server Error". What is wrong?
My folder structure is as follows:
As you can see the files are not in the root. They are inside two app/views/ folders.
Because of that the pages are opening with big and ugly links.
www.site.com/app/views/acessorios.php
www.site.com/app/views/clientes.php
www.site.com/app/views/empresas.php
I wish I could open them like this:
www.site.com/acessorios.php OR www.site.com/acessorios
www.site.com/clientes.php OR www.site.com/clientes
www.site.com/empresas.php OR www.site.com/empresas
That is, without looking like app/views/ in the url.
1st solution: With your shown samples and attempts please try following .htaccess rules file. Make sure to place your .htaccess rules file along with your app folder(not inside it, along side it). Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /app/views/
##External redirect rules from here.
RewriteCond %{THE_REQUEST} \s/app/views/([^.]*\.php)\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]*\.php)/?$ /app/views/$1 [QSA,L]
2nd solution: Rules to redirect URLs to www.site.com/acessorios are as follows:
RewriteEngine ON
RewriteBase /app/views/
##External redirect rules from here.
RewriteCond %{THE_REQUEST} \s/app/views/([^.]*)\.php\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /app/views/$1.php [QSA,L]

.htaccess rewrite cond/rule and hide url paremeters

I got a htaccess in my Root directory.
My files path http://example.com/folder1/folder2/index.php?country=eu&lang=en
I made this in htaccess.
RewriteCond %{HTTP_HOST} ^(www.)?anothersite.com$
RewriteRule ^([^/]*)/([^/]*)$ /folder1/folder2/index.php?country=$1&lang=$2 [L]
RewriteRule ^([^/]*)$ /folder1/folder2/index.php [L]
And looks like this
http://example.com/index.php?country=eu&lang=en
But i want like this -> http://example.com/eu-en
Is there a way to do this in htaccess?
EDIT:
I have to use rewrite cond, because im redirecting to folders depends of host.
EDIT 2: SOLUTION
RewriteCond %{HTTP_HOST} ^(www.)?anothersite.com$
RewriteRule ^([a-z]{2,2})-([a-zA-Z0-9_-]+)$ /folder1/folder2/index.php?country=$1&lang=$2 [QSA]
Thanks to answers, it is working now. But after that i got the css and js errors on website. Then i changed css and js paths /folder1/folder2/css.
You need to append with the [QSA] (query string append) tag. Try
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?lang=$1&page=$2 [QSA]
See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Try with below,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)-([^/]+)$ index.php?country=$1&lang=$2 [QSA]

htaccess remove folder redirect

I have a problem removing folders from an url. I want that google / old links aren't broken. The old webpage had several sections with a structure like this
example.com/news/items/entry1.html
example.com/news/items/entry2.html
example.com/blog/items/foo.html
The new page has the urls like this:
example.com/news/entry1
example.com/news/entry2
example.com/blog/foo
Removing html was rather straight forward
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php [QSA,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
</IfModule>
The part I'm struggling with is removing the 'items' part. The rules I found only worked for request path like 'example.com/items/subfolder1/...'
Any help would be greatly appreciated.
To remove /items/ from your URLs you can use the following in your .htaccess file:
RewriteEngine On
RewriteRule ^(.*)/items /$1 [L,R=301]
So for example, this will take the URL: http://example.com/news/items/entry1 and turn it into http://example.com/news/entry1
Make sure you clear your cache before testing this.

Rewriting ends up with not loading js / css

With the following .htaccess file, I obviously, get alot of 404's on loading scripts and css because the browser keeps looking in the wrong directory.
I am a newb at htaccess and have no clue about how to fix it.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^$ client/app/index\.html [L]
</IfModule>
Dir structure is as follows:
project /client / app / index.html
.htaccess is located in project directory.
I recomend you that redirect users with a 301 to correct path. This will end with your problems.
RewriteRule ^$ client/app/index\.html [L, 301]
If you are worried about seo look at https://support.google.com/webmasters/answer/93633?hl=en
All of the calls at your server are being redirected to your RewriteRule. Add a RewriteCond (RewriteCondition) to ignore CSS and JS files, if they exist:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^$ client/app/index\.html [L]
Explanation:
RewriteCond %{REQUEST_FILENAME} !-f
If the requested filename exists (css/main.css), do not go through with the RewriteRules.
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
If the file does not exist, but matches one of the extensions, do nothing. This is helpful because you get a 404 if css/main-2.css does not exist, rather than redirecting you to index.php.
And then finally is your own rule, which redirects all non-conditioned rules.
In your .htaccess file, this is what worked for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^ /index.html [R=301]
Thank you!

Rewriting folder won't work

I have an app stored on my server in "SERVER_ROOT/app/1.0.1/index.php". The app is accessed by typing in "example.com/index.php" and I use a rewrite to achieve this.
The issue I am having is I want users to access "SERVER_ROOT/app/1.0.1/image/" by typing "example.com/image/" but none of my rewrites work when I edit the .htaccess file.
Here is my .htaccess file:
Options -MultiViews
RewriteEngine on
RewriteBase /app/1.0.1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) index.php/$1 [L]
RewriteRule ^image/$ app/1.0.1/image/ [NC,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thanks,
Peter
Since the /image/ directory doesn't actually exist, it'll get trapped by the first rule that routes everything to index.php. Assuming your htaccess file is in your document root, you need to add the rule above your routing rule and change it to this:
RewriteRule ^image/(.*)$ image/$1 [L]
If your htaccess file isn't in the document root, then you need to add this rule to the top of the htaccess file in your document root:
RewriteRule ^image/(.*)$ /app/1.0.1/image/$1 [L]