Apache mod_rewrite and regular expression - php

In my HTML I want to refer to my images like this:
/images/articles/this-is-the-page-title-dgj4klci5j4.jpg
/images/articles/thumbs/this-is-the-page-title-dgj4klci5j4.jpg
/images/gallery/this-is-the-page-title-qj3k56l6kfm.jpg
but they are really located at:
/_pics/articles/dgj4klci5j4.jpg
/_pics/articles/thumbs/dgj4klci5j4.jpg
/_pics/gallery/qj3k56l6kfm.jpg
My .htaccess file is currently in the root directory of the site and looks something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website\.com [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [R=permanent,L]
RewriteRule ^articles/ user/articles.php [L]
RewriteRule ^gallery/ info/gallery.php [L]
I have to do this because in the last possible moment the client decided that he wants more legible image filenames and this seems like a quick reasonable fix.
I imagine that I could redirect everything from /images to /_pics and take the page title out by just using mod_rewrite. Problem is that I'm not very good with regular expressions and I don't fully understand the way mod_rewrite works. Can I get some help? Thanks
The real image filename only contains letters and numbers and can be jpg or png.

Try adding this rule in the htaccess file:
RewriteRule ^images/(.+)/[^/]+-([^/-]+)\.(jpe?g|gif|png)$ /_pics/$1/$2.$3 [L]

Related

I want to rewrite the url in corephp using .htaccess

http://localhost/rankeyiansacademy/course_details?id=digital-marketing-training
to
http://localhost/rankeyiansacademy/courses/digital-marketing-training
How can I change the url using htaccess in corephp?
I am hoping on your wamp/xampp rewrite module is on as required.
Then define $base_url = http://localhost/rankeyiansacademy in your config file or <base href="http://localhost/rankeyiansacademy/"> tag in html.
In your .htaccess file, add following code
RewriteRule ^courses/([A-Za-z-]+)$ course_details.php?id=$1
Then change the links in html like
If you define base tag, then use the following changes in html
Link
If you define variable in your config file,
Link
This one works for me. Hope that helps.
If you are looking for url rewrite, then you can use the Apache mod_rewrite module.
Apart from core php, if we use cms frameworks like wordpress, drupal, cakePHP, Zend, expression engine frameworks will implement url rewriting very easily.
Search engines like clean URLs pretty much and you should consider implementing it.
You can use this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
This can help you:
Write below code in your .htaccess file.
For detail page write: RewriteRule ^courses/([^/]+) course_details.php?id=$1 [NC]
For all course page: RewriteRule ^courses courses.php [NC]
Hope this can help you!!!
You can Use URL rewrite in htaccess File.
Put .htaccess file in main folder and use below code.It will read like if your URL http://example.com/index.php?page=search it will show like this http://example.com/search. This may help you to find solution
Code:
RewriteEngine on
RewriteCond %{THE_REQUEST} /product\.php\?cat=(search)
RewriteRule ^ /%1? [R=302,L]
RewriteRule ^(search)$ /product.php?page=$1 [L,QSA]

Rewrite/hiding some URL in .htaccess

I'm trying to get right syntax for .htaccess without any result...
I've a URL structured as domain.com/app/public/pageName .
It's working fine but I would "hide" the 'app/public/' part in browsers, basically doing something like:
[real URL] domain.com/app/public/pageName -> domain.com/pageName [what users type and see in browsers]
I think in that way it should be more readable and seo-friendly.
As I understood from docs (and maybe it's wrong because it's not working...) I should tell to Apache to map/redirect all URL like domain.com/pageName to domain.com/app/public/pageName , but only internally, in order to show the minimal URL in users' browsers.
Right now I have something like:
RewriteEngine on
#RewriteBase /app/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ https://localhost/app/public/index.php?url=$1 [QSA,L]
(I'm using full URL with https://... in order to get something that will be quick and easy to adapt when I upload all to my hosting, is it right?).
Problem is that RewriteRule actually change the URL, because it perform a redirect and URL rewrite it's not handle internally.
So, first of all: is it possible what I'm trying to do? If so, how can I handle the URL rewrite only internally?
Everything should be uploaded to a shared hosting, so I don't have other than .htaccess.
Anyway, I can consider to upgrade to a vps if there are not other possibilities...
Thanks!
==============
EDIT (should be more clear now)
tl;dr version:
I'm looking for a method that let users to type domain.com/pageName (and they will see that address in their browsers) and rewrite internally that URL in order to point to domain.com/app/public/pageName.
==============
More: after /app/public/ there can be an arbitrary number of elements, separated by / . All of these elements are appended at the end of the URL after index.php. At the end URL looks like:
domain/app/public/index.php?url=lot/of/elements/here
This is already working with the RewriteRule posted above, I would keep that too.
Thanks!
This is working fine for me, Hope it will work for you as well.
Check .htaccess here
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/app/public
RewriteRule ^app/public/(.*)$ /$1 [L,QSA]
Just for reference, I found a solution, maybe will be usefull for someone.
Basically I moved .htaccess to the root server (instead of /app/public directory) and changed the RewriteRule as follow:
RewriteEngine on
RewriteBase /app/public/
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT]
Now it's working (at least on localhost).
What do you think? Are there any side effects with this config?

RewriteRule will not work in .htaccess file

I have found several questions on Stack that have show lots and lot of examples, but I have tried everyone in the book and this refuses to work.
I am trying to convert.
www.domain.com/title/index.php?id=70048305
to
www.domain.com/title/70048305
I want it to be converted both ways. When they type in with the query string and with the clean url.
What is wrong with my rewrite?
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^title/(.+)$ index.php?id=$1 [L,QSA]
I should note that I've placed this .htaccess file both on the root and in the /title/ folder.
Try this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-+/]+)$ index.php?id=$1
RewriteRule ^([a-zA-Z0-9-+/]+)/$ index.php?id=$1
Don't match ^title/(.+)$ you need to be matching only numbers like so ^title/([\d]+)/?
Otherwise,
www.domain.com/title/index.php?id=70048305
will match and be redirected to:
index.php?id=index.php?id=70048305

.htacces css/js subdirectory issue

I am trying to make some changes to .htaccess file of a site. I have searched all over the web, but I cant find a clear solution for that, or there is possibility that I have got it wrong. So here we are are:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/$ /e/www/ [R]
RewriteRule ^news$ /news.php
RewriteRule ^news$ /news.php
RewriteRule ^resources$ /resources.php
RewriteRule ^lexicon$ /lexicon.php
RewriteRule ^contacts$ /contacts.php
RewriteRule ^analytics$ /analytics.php
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)-(.*)$ /news.php?yy=$1&mm=$2&dd=$3&alias=$4
RewriteRule ^resources$ /contacts.php
RewriteRule ^articles_search$ /articles_search.php
RewriteRule ^articles_data_search$ /articles_data_search.php
RewriteRule ^profile/([0-9]+)&commit=(.*)$ /profile.php?id=$1&commit=$2
RewriteRule ^profile/(.*)&commit=(.*)$ /profile.php?alias=$1&commit=$2
At last 2 rows, i am trying to have a result from this:
www.example.com/profile.php?alias=kokazani&commit=Edit
to this:
www.example.com/profile/kokazani&commit=Edit
The problem now is the wrong path of css and js files. I need a solution, or a better approach of rewriting rules. I think that a solution ,maybe, is to rewrite everything like css/blabla.css to root directory/css/blabla.css, but if there a standard-solution foro this problem, i would like to know :)
Thanks in advance.
There are 2 possible (simple) solutions:
Use absolute paths for your CSS files (/include/css/style.css etc)
Use HTML's <base> element to set the correct base URL for relative URLs to follow (rather then the directory of the document which is altered by Apache).
My approach would be (as suggested by Truth) using absolute paths. If this is not possible, you could use:
RewriteRule ^(.+)/css/(.*)$ /css/$2
But be warned: This means the client (browser) will end up getting and caching multiple copies of the same file because it is fetching it (as far as it can see) from different paths. Therefore you lose the advantage of client-side stylesheet caching.

.htaccess RewriteEngine breaks CSS/ Images

I successfully was able to rewrite my dirty urls to clean urls but this broke my image and style folders too :(
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /www/index.php?page=$1&cmd=$2 [L]
Add the follwing rewrite conditions to prevent rewriting of URLs that lead to existing files and directories, e.g. your JS, CSS and image files.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /www/index.php?page=$1&cmd=$2 [L]
If your images still don't show up, it's probably because you're including images like this: <img src="./img/myimage.png" />. If you now point your browser to http://site.com/page/hello/, your browser will be looking here for the image: http://site.com/page/hello/img/myimage.png. However, the image is actually here: http://site.com/img/myimage.png.
So you'll have to include your images like this: <img src="/img/myimage.png" /> (everything after the domain).
This will force the browser to look ìn the correct place. (Assuming that your directory structure is like this.)
That's probably because your styles are going to index.php too now. What I mean is
/styles/myStyle.css -> /www/index.php?page=styles&cmd=myStyle.css
So you should probably fix this by only rewriting some URLs. Like something like this?
RewriteRule ^pages/([^/]*)/([^/]*)$ /www/index.php?page=$1&cmd=$2 [L]

Categories